954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Word count

Hello

I would like to count how many words are in a string

ie "PETER JOHNSON LTD"

so this string contains 3

I was going to use strtok but this is too slow.

Any other ideas?

sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
 

Go over the string and count spaces. The number of words is one more than the number of spaces in the string if the string isn't empty.

#include <stdio.h>
#include <ctype.h>

int main( void ) {
  char string[100];
  int i;
  int count = 0;

  printf( "Enter a sentence> " );
  fgets( string, 100, stdin );
  string[strlen( string ) - 1] = '\0';

  /* count the words */
  for ( i = 0; string[i]; ++i ) {
    if ( isspace( string[i] ) ) {
      ++count;
    }
  }

  if ( i > 0 ) {
    ++count;
  }

  printf( "\"%s\" has %d words\n", string, count );

  return 0;
}
Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

>>Go over the string and count spaces
Generally, yes, but you also have to check if words are separated by more than one white space (spaces or tabs). When a white space character is encountered you have to increment the loop counter until the first non-white space character or end-of-string is found before continuing the loop. If you don't, the word count might be wrong.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Try something like this. It would work for any kind of whitespaces (newline, spaces, tabs).

int main(void)
{
    char str[] = " hello \n   to all \n\r\f the \t people ";
    int limit = strlen(str);
    int i, change = 1, words = 0;
    for(i = 0; i < limit; ++i)
    {
        if(!isspace(str[i]))
        {
            if(change)
            {
                ++words;
                change = 0;
            }
        }
        else
        {
            change = 1;
        }
    }
    printf("Word count: %d\n", words);
    getchar();
    return(0);
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

both are nice answers...

anupam_smart
Posting Pro
599 posts since Feb 2006
Reputation Points: 15
Solved Threads: 4
 

I'd use strtok(). If it is slower, it isn't much slower. It is also neater, seeing as how it won't take as many lines of code and variables.

MellyGSter
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Just in case this problem has been bugging you after the past 2 and a half years.

MellyGSter
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Here's mine

int wordCount( char *line )
{
  int count = 0 ;
  
  // advance until the first nonwhitespace character
  while( isspace( *line ) )  line++ ;

  int len = strlen( line ) ;
  bool onWord = true ;
  
  for( int i = 0 ; i < len; i++ )
  {
    if( isspace( line[i] ) )
    {
      // INcrement count only when
      // stepping OFF word to whitespace
      if( onWord )
        count++ ;
      onWord = false ;
    }
    else
    {
      onWord = true ;
    }

  }

  // If you ended while still on word
  // then increment count here to count
  // the "last" word
  if( onWord )
    count++ ;

  return count ;

}

int main()
{
  char *line1 = "a line of words" ;
  char *line2 = "Hi ther e     !!  Each glob of    characters\n"
  "counts as a     word \t \t \t \n" ;
  char *line3 = "Here's   another long    line of 3 4 6 6  8 " ;

  printf( "%d words in `%s`\n\n", wordCount(line1), line1 ) ;
  printf( "%d words in `%s`\n\n", wordCount(line2), line2 ) ;
  printf( "%d words in `%s`\n\n", wordCount(line3), line3 ) ;
}
billybaloop
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

well, okay...... but we waited almost 3 years for that?

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
#include<stdio.h>

int main()
{

   char name[20];
   int i, j, count = 0, end = 0, line = 0, word = 0, flag = 0; 
   printf ( "Enter the strings\n" );
   gets ( name ); 

   for ( i = 0; name[i] != '\0'; i++ )
   {
      if ( name[i] == ' ' || name[i] == '\t' )
         printf ( " " );
      count++;

      if ( name[i] == '\0' )
         end = 0;
      else if ( end == 0 )
      {
         end = 1;
         line++;
      }
      if ( name[i] == ' ' || name[i] == '\t' || name[i] == '\n' )
         flag = 0;
      else if ( flag == 0 )
      {
         flag = 1;
         word++;
      }

   } 

   printf ( "Total number of characters %d\n", count );

   printf ( "Total number of word %d\n", word );

   printf ( "Total number of line %d\n", line );

}
SumitGemini0
Newbie Poster
2 posts since May 2010
Reputation Points: 5
Solved Threads: 0
 

^^ At above poster. No.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

alright, someone close this tard-magnet thread already.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: