Advertisement
Guest User

K&C 1-18

a guest
Feb 21st, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* K&C, Execise 1-18: Write a program to remove
  4.    trailing blanks and tabs from each line of
  5.    input, and to delete entirely blank lines.
  6.    
  7.    I assume we print the lines at some point too. */
  8.  
  9. // removing trailing blanks and tabs must be done on char by char basis.
  10. // removing entirely blank lines could be done in a number of ways, but
  11. // i want to do it in a line by line basis.
  12.  
  13. int printline(int lim);
  14.  
  15. //ignores everything after spaces...
  16.  
  17. int main()
  18. {
  19.    printline(2000);
  20.    return 0;
  21. }
  22.  
  23. // printline: print a line minus excess whitespace
  24. int printline(int lim)
  25. {
  26.    char s[lim];
  27.    int c, i, prev;
  28.    prev = 0;
  29.    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  30.    {//remove i < lim - 1 to get multiple lines?
  31.       if (prev == 0)
  32.       {//case 1: first character
  33.          if (c >= 33)
  34.          {//case 1a: first character is not whitespace
  35.             s[i] = c;   //thus add it to array
  36.             prev = c;   //thus make a note of it
  37.          }
  38.          else if (c < 33)
  39.          {//case 1b: first character is whitespace
  40.             prev = c;   //thus make a note of it
  41.          }
  42.       }
  43.       else if (prev >= 33)
  44.       {//case 2: previous character is not whitespace
  45.          if (c >= 33)
  46.          {//case 2a: current character is not whitespace
  47.             s[i] = c;   //thus add it to array
  48.          }
  49.          else if (c < 33)
  50.          {//case 2b: current character is whitespace
  51.             prev = c;   //thus make a note of it
  52.          }
  53.       }
  54.       else if (prev < 33)
  55.       {//case 3: previous character is whitespace
  56.          if (c >= 33)
  57.          {//case 3a: current character is not whitespace
  58.             s[i] = c;   //thus add it to array
  59.             prev = c;   //thus make a note of it
  60.          }
  61.          else if (c < 33)
  62.          {//case 3b: current character is whitespace
  63.             ;           //nothing of note changed, carry on
  64.          }
  65.       }
  66.       /*if ((prev == 0) && (c >= 33))
  67.       {//case 1: first char is not whitespace
  68.          s[i] = c;
  69.          prev = c;
  70.       }
  71.       else if (prev >= 33 && c >= 33)
  72.       {//case 2: previous character was not whitespace and current character not whitespace
  73.          s[i] = c;
  74.          prev = c;
  75.       }
  76.       else if ((prev < 33) && (c < 33))
  77.       {//case 3: current character is white space and previous character is whitespace
  78.          prev = c;
  79.       }
  80.       else if ((prev < 33) && (c >= 33))
  81.       {//case 4: current character is not white space but previous character is whitespace
  82.          s[i] = c;
  83.          prev = c;
  84.       }*/
  85.    }
  86.    if (c == '\n')
  87.    {//add a newline character and increment counter
  88.       s[i] = c;
  89.       ++i;
  90.    }
  91.    s[i] = '\0'; //add null terminator
  92.    if (i > 0)
  93.    {
  94.       printf("%s", s);
  95.    }
  96.    return i; //return length
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement