This programs counts the lines, words, and characters. Every time the program encounters the first characters of a word, it counts one more word. The variable 'state' records whether the program is currently in a word or not.
#include#define IN 1 #define OUT 0 main() { int c, nl, nw, nc, state; clrscr(); state = OUT; nl =nw =nc =0; while((c=getchar()) !=EOF){ /** count the character **/ ++nc; /** count the line **/ if(c ==' ' || c == '\n' || c =='\t') state = OUT; else if(state == OUT){ state = IN; /** count the word **/ ++nw; } } printf("Number Of Line =%d\n",nl); printf("Number Of Word =%d\n",nw); printf("Number Of Character =%d\n",nc); getch(); }
Output
[caption id="attachment_1534" align="aligncenter" width="643"]