/* * trim_strings.c * * Remove trailing blanks and tabs from each line of input, and * delete entirely blank lines. * K&R Exercise 1.18 */ #include #include #include // Maximum input line size #define MAXLINE 256 // Function that trims the whitespace at the end of string s // and returns the length of the new string. int trim_string (char s[]); int main() { char line[MAXLINE]; int line_length; // Read input line-by-line while (fgets(line, MAXLINE, stdin) != NULL) { // Trim whitespace and print line if any characters left line_length = trim_string(line); if (line_length > 0) printf("|%s|\n", line); } return 0; } int trim_string (char s[]) { // Implementation of the trim_string function int i = strlen(s); while(i > 0 && isspace(s[i-1])) { s[i-1] = '\0'; i--; } return i; }