This function determines the number of words in a string.

You're viewing an older version of this page (#5608). View the current version.

View versions (2)

Interface

#include <codecogs/strings/word_count.h>

using namespace Strings;

The assumptions involved are:

  • The string is null terminated
  • The words separation char is only a space and nothing else.

Example 1

#include <cstdio>
#include<codecogs/strings/word_count.h>
int main () {
   char * sz = "The quick     brown fox jumps over the lazy           dog.";
   int nWordCount = Strings::word_count (sz);
   printf ("The word count is %d", nWordCount);
   return 0;
}

Output:

9

Parameters

text
The null terminated input string

Returns

Number of words in the input string.
GPL Licence — free for non commercial use. See Licence details.