Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines

#include <stdio.h>
#define MAXLINE      1000    /*maximum input line size */

int getline(char line[], int maxline);
int remove(char s[]);

/* remove trailing blanks and tabs, and delete blank lines */
int main(){
    char line[MAXLINE];      /*current input line */

    while (getline(line, MAXLINE) > 0)
         if (remove(line) > 0)
              printf("%s", line);
         return 0;
}
/*remove trailing blanks and tabs from character string s*/
int remove(char s[]){
     int i;

      i = 0;
      while(s[i] != '\n')   /*find newline character */
            ++i;
      --i;                /*back off from '\n'    */
     while(i >= 0 && (s[i] == ' ' || s[i] == '\t'))
          --i;
     if (i >= 0){   /*is it a non blank line? */
         ++i;
          s[i] = '\n';             /*put newline character back  */
          ++i;
          s[i] = '\0';          /* terminate the string */
          
      }
      return i;
}

Leave a comment