I have a very simple program that I am not sure why it works. It should just count the number of lines, words, and characters that are typed. It counts the lines and characters properly but not the words.
Code:
#include <stdio.h>
#define IN 1 //inside a word
#define OUT 0 //outisde a word
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);
}