Untitled

                Never    
C
       
#include <stdio.h>

/* K&C, Execise 1-18: Write a program to remove
   trailing blanks and tabs from each line of 
   input, and to delete entirely blank lines.
   
   I assume we print the lines at some point too. */

// removing trailing blanks and tabs must be done on char by char basis.
// removing entirely blank lines could be done in a number of ways, but
// i want to do it in a line by line basis.

int printline(int lim);

//ignores everything after spaces...

int main()
{
   int keepGoing;
   keepGoing = 1;
   while (keepGoing > 0)
   {// EOF is 0, uncomment below to see
      keepGoing = printline(2000);
      //printf("%d\n", keepGoing);
   }
   return 0;
}

// printline: print a line minus excess whitespace
int printline(int lim)
{
   char s[lim];
   int c, i, prev;
   for (i = 0; i < lim; i++)
   {
      s[i] = 0;
   }
   prev = 0;
   for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
   {//remove i < lim - 1 to get multiple lines?
      if (prev == 0)
      {                 //case 1: first character
         if (c >= '!')  //replace this for checks of space and tab, as with all other comparisons similar to it
         {              //case 1a: first character is not whitespace
            s[i] = c;   //thus add it to array
            prev = c;   //... and make a note of it
         }
         else if (c < '!')
         {              //case 1b: first character is whitespace
            prev = c;   //thus make a note of it, but don't add it to array.
            i--;
         }
      }
      else if (prev >= '!')
      {                 //case 2: previous character is not whitespace
         if (c >= '!')
         {              //case 2a: current character is not whitespace
            s[i] = c;   //thus add it to array
         }
         else if (c < '!')
         {              //case 2b: current character is whitespace
            if (c == ' ')
            {              //is it a space?
               s[i] = c;   //if it is, add it to the array
               prev = c;   //... and make a note of it
            }
            else if (c == '\t')
            {
               s[i] = ' ';
               prev = c;
            }
         }
      }
      else if (prev < '!')
      {//case 3: previous character is whitespace
         if (c >= '!')
         {//case 3a: current character is not whitespace
            s[i] = c;   //thus add it to array
            prev = c;   //... and make a note of it
         }
         else if (c < '!')
         {//case 3b: current character is whitespace
            prev = c;           //nothing of note changed, carry on
            i--;
         }
      }
   }
   if (c == '\n')
   {//add a newline character and increment counter
      s[i] = c;
      ++i;
   }
   s[i] = '\0'; //add null terminator
   if (i > 0)
   {
      printf("%s", s);
   }
   return i; //return length
}

Raw Text