Explain fold.c

This commit is contained in:
caskd 2019-10-21 19:19:09 +02:00
parent fdf2e04a3f
commit 858c80c23b
No known key found for this signature in database
GPG Key ID: 79DB21404E300A27
1 changed files with 6 additions and 0 deletions

View File

@ -3,12 +3,18 @@
int fold(char s[], int lim, int cpr) {
int c, i;
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')) {
/* Remove spaces if we are at the first column, else keep it */
s[i] = c;
++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--);
/* Failsafe in case if no input is given. */
if (i > 0) {
s[i] = '\n';
s[i+1] = '\0';