aboutsummaryrefslogtreecommitdiffstats
path: root/rtrailws.c
blob: a1d1956fd8ae7669b05ed641eb2dc2e19a46c341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Remove trailing whitespace.
#include<stdio.h>

int main (int argc, char *argv[]) {
    char c;
    int spacecount = 0;
    while ( (c = getchar()) != EOF ) {
        if (c == ' ') {
            spacecount++;
        } else if (c == '\n' && spacecount) {
            putchar('\n');
            spacecount = 0;
        } else {
            if (spacecount) {
                int i;
                for (i = 0; i < spacecount; i++)
                    putchar(' ');
                spacecount = 0;
            }
            putchar(c);
        }
    }
}