Counting WORDS in a String?

How can i count words in a string? so far im totally doing it wrong, i really need help! it has to follow the while format!

char mess[50],word=0;
int len,i=0;

printf("Enter Message: ");


while (i<=len){
  if (mess[i]>='a' && mess[i]<='z')
  else if (mess[i]>='A' && mess[i]<='Z')

printf("Total Number of Words: %d\n",word);             

Last edited on
Words are generally delimited by white space.
but if i just count the spaces, if someone double spaces it would give me the wrong output... I need a sample code to atleast start it out or the code itself.

btw thanks for the quick reply!
Last edited on
First of all, don't try to reinvent the wheel:

This is a standard header file that contains a couple of functions that you will find useful for this task. Checking for consecutive white space is as simple as checking whether the previous or the next character in the sequence is also a white space. Don't forget to check your bounds when doing this.

Secondly, if you have the time, learn how the I\O buffer works. The iostream (and maybe the string) header(s) is\are better suited for this kind of work then stdio.h is. All you should need are the extraction operator and the eof() function.
our professor wants us to do this in string & while loop format in stdio.h and string.h. hes irrational but hes the boss. Which is why I really need serious help
Ah, the old "I'm going to teach you to do this by making you do it wrong" routine. In that case your example is right here:

But instead of outputting the token you'd be incrementing 'word' (which should NOT be a char by the way).
It might be better to look for letters and non-letters, rather than spaces. Then you don't have to worry about punctuation either.

Then, as you know that a word begins with the first letter after a non-letter and has ended by the first non-letter, you can (easily!) arrange to increment the word count only when you start a new word.

Ideally you would use lines 14 and 16 to create a function to use to test the chars with (there is a standard function for this purpose, isalpha(), but I assume you're not allowed to use it here if you're only allowed to use stdio.h and string.h)

But you should at least merge line 14 and 17 using ||

And as C strings are null-terminated, you don't need to use strlen to solve this problem.


PS How many words are in this sentence?

"It's a self-evident, all-inclusive cross-reference to Darwin's theory of evolution?"
Last edited on
@ andy: Why would you look for non-letters? A number written as 2 is still a word in the seance that it is a character or set of characters that has a meaning. Maybe this is something that the OP needs to clear up with their instructor?
Last edited on

You said:
The iostream (and maybe the string) header(s) is\are better suited for this kind of work then stdio.h is. All you should need are the extraction operator and the eof() function.

Can you please elaborate a bit more about how exactly the extraction operator and eof() are useful?

The extraction operator tosses out white space for you by default whether it's consecutive or not. So you'd be counting how many times you read from std::cin until eof is reached. Now that I think about it though it would probably turn into a mess really quick since the extraction operator would leave the CRLF command in the istream.

EDIT: You can make it work but you need to also use std::cin.peek().
Last edited on
Computergeek wrote:
Why would you look for non-letters?

I was focused on the simple, normal words case. Strictly speaking "10 green bottles" has two words and one number.

But the same approach can be used with is-in / is-not-in {the set of chars which is found in words}; I would still not bother to look for spaces.

Last edited on
@ Andy: OK, I could see a scenario where if the instructor does not consider an ellipse or a number to be a word then your approach would be better; otherwise every other form of punctuation is found adjacent to a word and would be considered part of it so it wouldn't throw off the count (they're modifiers after all). It just seemed to be more complex then it needed to be at first.

EDIT: Sorry, I'm playing around with writing a grammar checker and I'm delimiting on white space, your post made me think that might have been a mistake.
Last edited on
Topic archived. No new replies allowed.