943,918 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9153
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 18th, 2007
0

Word count

Expand Post »
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?
Similar Threads
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Jul 18th, 2007
0

Re: Word count

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.
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int main( void ) {
  5. char string[100];
  6. int i;
  7. int count = 0;
  8.  
  9. printf( "Enter a sentence> " );
  10. fgets( string, 100, stdin );
  11. string[strlen( string ) - 1] = '\0';
  12.  
  13. /* count the words */
  14. for ( i = 0; string[i]; ++i ) {
  15. if ( isspace( string[i] ) ) {
  16. ++count;
  17. }
  18. }
  19.  
  20. if ( i > 0 ) {
  21. ++count;
  22. }
  23.  
  24. printf( "\"%s\" has %d words\n", string, count );
  25.  
  26. return 0;
  27. }
Last edited by Hamrick; Jul 18th, 2007 at 11:05 am.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 18th, 2007
0

Re: Word count

>>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.
Last edited by Ancient Dragon; Jul 18th, 2007 at 12:28 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jul 18th, 2007
0

Re: Word count

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

  1. int main(void)
  2. {
  3. char str[] = " hello \n to all \n\r\f the \t people ";
  4. int limit = strlen(str);
  5. int i, change = 1, words = 0;
  6. for(i = 0; i < limit; ++i)
  7. {
  8. if(!isspace(str[i]))
  9. {
  10. if(change)
  11. {
  12. ++words;
  13. change = 0;
  14. }
  15. }
  16. else
  17. {
  18. change = 1;
  19. }
  20. }
  21. printf("Word count: %d\n", words);
  22. getchar();
  23. return(0);
  24. }
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jul 21st, 2007
0

Re: Word count

both are nice answers...
Reputation Points: 15
Solved Threads: 4
Posting Pro
anupam_smart is offline Offline
598 posts
since Feb 2006
Feb 25th, 2010
-1
Re: Word count
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MellyGSter is offline Offline
2 posts
since Feb 2010
Feb 25th, 2010
0
Re: Word count
Just in case this problem has been bugging you after the past 2 and a half years.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MellyGSter is offline Offline
2 posts
since Feb 2010
Apr 20th, 2010
-2
Re: Word count
Here's mine

  1. int wordCount( char *line )
  2. {
  3. int count = 0 ;
  4.  
  5. // advance until the first nonwhitespace character
  6. while( isspace( *line ) ) line++ ;
  7.  
  8. int len = strlen( line ) ;
  9. bool onWord = true ;
  10.  
  11. for( int i = 0 ; i < len; i++ )
  12. {
  13. if( isspace( line[i] ) )
  14. {
  15. // INcrement count only when
  16. // stepping OFF word to whitespace
  17. if( onWord )
  18. count++ ;
  19. onWord = false ;
  20. }
  21. else
  22. {
  23. onWord = true ;
  24. }
  25.  
  26. }
  27.  
  28. // If you ended while still on word
  29. // then increment count here to count
  30. // the "last" word
  31. if( onWord )
  32. count++ ;
  33.  
  34. return count ;
  35.  
  36. }
  37.  
  38. int main()
  39. {
  40. char *line1 = "a line of words" ;
  41. char *line2 = "Hi ther e !! Each glob of characters\n"
  42. "counts as a word \t \t \t \n" ;
  43. char *line3 = "Here's another long line of 3 4 6 6 8 " ;
  44.  
  45. printf( "%d words in `%s`\n\n", wordCount(line1), line1 ) ;
  46. printf( "%d words in `%s`\n\n", wordCount(line2), line2 ) ;
  47. printf( "%d words in `%s`\n\n", wordCount(line3), line3 ) ;
  48. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
billybaloop is offline Offline
1 posts
since Mar 2010
Apr 20th, 2010
1
Re: Word count
well, okay...... but we waited almost 3 years for that?
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 23rd, 2010
-1

How to count words, line and character in a single String

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.  
  6. char name[20];
  7. int i, j, count = 0, end = 0, line = 0, word = 0, flag = 0;
  8. printf ( "Enter the strings\n" );
  9. gets ( name );
  10.  
  11. for ( i = 0; name[i] != '\0'; i++ )
  12. {
  13. if ( name[i] == ' ' || name[i] == '\t' )
  14. printf ( " " );
  15. count++;
  16.  
  17. if ( name[i] == '\0' )
  18. end = 0;
  19. else if ( end == 0 )
  20. {
  21. end = 1;
  22. line++;
  23. }
  24. if ( name[i] == ' ' || name[i] == '\t' || name[i] == '\n' )
  25. flag = 0;
  26. else if ( flag == 0 )
  27. {
  28. flag = 1;
  29. word++;
  30. }
  31.  
  32. }
  33.  
  34. printf ( "Total number of characters %d\n", count );
  35.  
  36. printf ( "Total number of word %d\n", word );
  37.  
  38. printf ( "Total number of line %d\n", line );
  39.  
  40. }
Last edited by adatapost; May 24th, 2010 at 5:22 am. Reason: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).
Reputation Points: 5
Solved Threads: 0
Newbie Poster
SumitGemini0 is offline Offline
2 posts
since May 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: can i use strtok to assign the tokens to different char pointers?
Next Thread in C Forum Timeline: Copy Directory to Other Directory





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC