#include #define IN 1 /* inside of a word */ #define OUT 0 /* outside of a word */ /* count lines, words, and characters */ void wc() { /* * c must be big enough to hold any value getchar() returns so we use * int instead of char to hold EOF, an integer defined in stdio */ int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') { ++nl; } /* && is higher precedence than || */ if (c == ' ' || c == '\t' || c == '\n') { state = OUT; } else if (state == OUT) { state = IN; ++nw; } } printf("%d %d %d\n", nl, nw, nc); } /* print input separated by new lines */ void print_word_line() { int c; while ((c = getchar()) != EOF) { if (c == ' ' || c == '\t' || c == '\n') { putchar('\n'); } else { putchar(c); } } } int main() { wc(); /* print_word_line(); */ return 0; }