Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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()
- {
- printline(2000);
- return 0;
- }
- // printline: print a line minus excess whitespace
- int printline(int lim)
- {
- char s[lim];
- int c, i, prev;
- 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 >= 33)
- {//case 1a: first character is not whitespace
- s[i] = c; //thus add it to array
- prev = c; //thus make a note of it
- }
- else if (c < 33)
- {//case 1b: first character is whitespace
- prev = c; //thus make a note of it
- }
- }
- else if (prev >= 33)
- {//case 2: previous character is not whitespace
- if (c >= 33)
- {//case 2a: current character is not whitespace
- s[i] = c; //thus add it to array
- }
- else if (c < 33)
- {//case 2b: current character is whitespace
- prev = c; //thus make a note of it
- }
- }
- else if (prev < 33)
- {//case 3: previous character is whitespace
- if (c >= 33)
- {//case 3a: current character is not whitespace
- s[i] = c; //thus add it to array
- prev = c; //thus make a note of it
- }
- else if (c < 33)
- {//case 3b: current character is whitespace
- ; //nothing of note changed, carry on
- }
- }
- /*if ((prev == 0) && (c >= 33))
- {//case 1: first char is not whitespace
- s[i] = c;
- prev = c;
- }
- else if (prev >= 33 && c >= 33)
- {//case 2: previous character was not whitespace and current character not whitespace
- s[i] = c;
- prev = c;
- }
- else if ((prev < 33) && (c < 33))
- {//case 3: current character is white space and previous character is whitespace
- prev = c;
- }
- else if ((prev < 33) && (c >= 33))
- {//case 4: current character is not white space but previous character is whitespace
- s[i] = c;
- prev = c;
- }*/
- }
- 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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement