I've been following K&R book 2nd version for c programming, but in 3.7 part, here is the screenshot of the function and my code part:
#include <stdio.h>
#include <string.h>
/* trim: removing the trailing blanks, tabs and newlines */
int trim(char s[]);
int main(){
char s[] = "hello,world \t\t\t\t\n\n\n\n";
printf("%s\n", s);
int length = trim(s);
printf("%d\n", length);
return 0;
}
int trim(char s[]){
int n;
for (n = strlen(s) -1 ; n >= 0 ; n--)
if (s[n] != ' ' && s[n] != '\n' && s[n] != '\t')
break;
s[n+1] = '\0';
return n;
}
The following is the output I get from running it:
Obviously the result length is 11, "hello,world", but the program outputs 10, the reason is s[n+1] = '\n', instead of s[++n];I think it should be s[++n], otherwise I will get the wrong output ==> 10, how to deal with it? could anyone plz take a look at it?

