Word count

Reply

Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Word count

 
0
  #1
Jul 18th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Word count

 
0
  #2
Jul 18th, 2007
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 10:05 am.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,605
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Word count

 
0
  #3
Jul 18th, 2007
>>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 11:28 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,771
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 493
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Word count

 
0
  #4
Jul 18th, 2007
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. }
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.

Dammit, Jones, What the Hell Are Knoll Pointers?!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 598
Reputation: anupam_smart is an unknown quantity at this point 
Solved Threads: 4
anupam_smart's Avatar
anupam_smart anupam_smart is offline Offline
Posting Pro

Re: Word count

 
0
  #5
Jul 21st, 2007
both are nice answers...
# Never say impossible 'cause impossible itself says:

"I M POSSIBLE":) :) :)

#Your I Can is more important than your I.Q.
Reply With Quote Quick reply to this message  
Join Date: Feb 2010
Posts: 2
Reputation: MellyGSter is an unknown quantity at this point 
Solved Threads: 0
MellyGSter MellyGSter is offline Offline
Newbie Poster
 
-1
  #6
23 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2010
Posts: 2
Reputation: MellyGSter is an unknown quantity at this point 
Solved Threads: 0
MellyGSter MellyGSter is offline Offline
Newbie Poster
 
0
  #7
23 Days Ago
Just in case this problem has been bugging you after the past 2 and a half years.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 3495 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC