word count program not working
Hi all,
I'm trying to create a word count program. With the definition that a word is any sequence of characters that does not contain a blank, tab or newline.
Every time the program encounters the first character of a new word, it should count a word.
Here's the code :
Code:
#include <stdio.h>
#define IN 1 // inside a word
#define OUT 0 // outside a word
// count lines, words and characters in input
int main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0; // nl = newline; nw = new word; nc = new character
while ((c = getchar()) != EOF)
{
nc = nc + 1;
if (c == '\n')
{
nl = nl + 1;
}
if (c == ' ' || c == '\n' || c == '\t')
{
state = OUT;
}
else if (state == OUT)
{
state = IN;
nw = nw + 1;
}
}
printf ("%d %d %d \n", nl, nw, nc);
}
Well...it's not working. I've been typing words but nothing happened. It just won't count...
Nothing happens, just blank.
Could someone please point where I did wrong? FYI I'm using a Visual C++ 2008 to compile the program.
Also, I'm a newbie so please bear with me :)
Thank you.