#include #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ /* count lines, words, and characters in input * * I would focus on testing the state transitions, since character and * line counting are straightforward. The character count increments for * every character that is not EOF, and the line count increments whenever * a newline is encountered. Word counting is more complex because it * depends on correctly detecting boundaries. I would include test cases * with varying numbers of spaces, tabs, and newlines to verify that word * boundaries are handled correctly, since those inputs are the most likely * to reveal bugs. */ int main() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { nc++; if (c == '\n') ++nl; if (c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("%d %d %d\n", nl, nw, nc); }