Explain fold.c
This commit is contained in:
parent
fdf2e04a3f
commit
858c80c23b
|
@ -3,12 +3,18 @@
|
||||||
int fold(char s[], int lim, int cpr) {
|
int fold(char s[], int lim, int cpr) {
|
||||||
int c, i;
|
int c, i;
|
||||||
for (i=0; i < lim-1 && i<cpr && (c=getchar())!=EOF && c!='\n';) {
|
for (i=0; i < lim-1 && i<cpr && (c=getchar())!=EOF && c!='\n';) {
|
||||||
|
/* We read the line and choose to increase or not if it is a space at the beginning
|
||||||
|
* with this, we do not have to read the buffer, we work with the current input directly.
|
||||||
|
*/
|
||||||
if (i != 0 || (c != ' ' && c != '\t')) {
|
if (i != 0 || (c != ' ' && c != '\t')) {
|
||||||
|
/* Remove spaces if we are at the first column, else keep it */
|
||||||
s[i] = c;
|
s[i] = c;
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* Once we have finished reading the buffer, check for trailing tabs and spaces and go back in the buffer to fill those. */
|
||||||
for (;i > 0 && (s[i-1] == ' ' || s[i-1] == '\t'); i--);
|
for (;i > 0 && (s[i-1] == ' ' || s[i-1] == '\t'); i--);
|
||||||
|
/* Failsafe in case if no input is given. */
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
s[i] = '\n';
|
s[i] = '\n';
|
||||||
s[i+1] = '\0';
|
s[i+1] = '\0';
|
||||||
|
|
Loading…
Reference in New Issue