/* wordcount.c * * Example C program for CS 101 * * to compile, use gcc wordcount.c -o wordcount */ #include #define TRUE 1 #define FALSE 0 int main() /* counts lines, words, and chars in input */ { int c, nl, nw, nc, inword; inword = FALSE; nl = nw = nc = 0; while ((c = getchar()) != EOF) { nc++; if (c == '\n') nl++; /* new line */ if (c == ' ' || c == '\n' || c == '\t') inword = FALSE; /* end of a word */ else if (inword == FALSE) { inword = TRUE; /* beginning of a new word */ nw++; } } printf("%d lines, %d words, %d characters\n", nl, nw, nc); return 0; }