exc/k&r/01-a-tutorial-introduction/01-18.c

38 lines
765 B
C

#include <stdio.h>
#define MAXBUFF 1000
/* removes trailing spaces and tabs */
int main()
{
int i, c;
char buff[MAXBUFF+1]; /* +1 fo \0 */
i = 0;
buff[MAXBUFF] = '\0';
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\t') {
if (i < MAXBUFF) {
buff[i] = c;
++i;
} else {
/* gracefully handle overflow */
printf("%s", buff);
i = 0;
}
} else if (c == '\n') {
buff[0] = c;
i = 1;
} else {
if (i > 0) {
buff[i] = '\0';
printf("%s", buff);
i = 0;
}
putchar(c);
}
}
return 0;
}