#include <stdio.h>
#define IN 1
#define OUT 0
/* count lines, words and characters in input */
int main()
{
int c, n1, nw, nc, state;
state = OUT;
n1 = nw = nc = 0;
while (( c = getchar()) !=EOF) {
++nc;
if (c == '\n')
++n1;
if ( c == ' ' || c == '\n' || c == '\t')
state = OUT;
elseif (state = OUT) {
state = IN;
++nw;
}
}
printf( "%d %d %d\n", n1, nw, nc);
}
Let me suggest some boundary conditions that may be useful:
- empty file
- file with all white space
- file with no white space
- file with one word followed by white space
- file with white space followed by one word.
If the file doesn't end in '\n', then should the last line be counted? Put another way, if there are no \n characters in the file, does it have 1 line or 0?