From c6070adb670cee86eb752ca5e8c2c8661be644bd Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Tue, 30 Nov 2021 01:43:24 -0500 Subject: [PATCH] Exercise 1-18. Remove trailing blanks and tabs --- 14-char-arrays.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/14-char-arrays.c b/14-char-arrays.c index b528e89..bcd8fa5 100644 --- a/14-char-arrays.c +++ b/14-char-arrays.c @@ -4,6 +4,7 @@ int getline(char line[], int maxline); void copy(char to[], char from[]); +void trim(char line[], int len); /* print longest input line */ main() @@ -22,8 +23,10 @@ main() if (c == '\n') ++len; } - if (len >= MINLINE) + if (len >= MINLINE) { + trim(line, len); printf("%s", line); + } } return 0; } @@ -52,3 +55,15 @@ void copy(char to[], char from[]) while ((to[i] = from[i]) != '\0') ++i; } + +/* trim: remove extraneous blanks and tabs */ +void trim(char s[], int len) +{ + int i; + + for (i = len-2; i >= 0; --i) + if (s[i] == ' ' || s[i] == '\t') + s[i] = '\0'; + else + return; +}