| | |
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.
C Syntax (Toggle Plain Text)
#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; }
Last edited by Hamrick; Jul 18th, 2007 at 10:05 am.
The truth does not change according to our ability to stomach it.
>>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.
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Try something like this. It would work for any kind of whitespaces (newline, spaces, tabs).
c Syntax (Toggle Plain Text)
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); }
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?!
Sacrifice is a painful, pure and beautiful thing.
Dammit, Jones, What the Hell Are Knoll Pointers?!
![]() |
Similar Threads
- word count (C++)
- Word count help. (C++)
- Can you please help me write this word count program in another way (Python)
- I Need Help Writing A Word Count Program In My Python (Python)
- Need Help, basic word count, string manipulation (VB.NET)
- word count in borland c++ ?? (C++)
- word count (Java)
- I can't implement a word count into my text editor (JAVA) (Java)
Other Threads in the C Forum
- Previous Thread: help about this one...
- Next Thread: Information
Views: 3495 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C
#include api array arrays binary binarysearch bit c++ calling char character code coke command conversion convert database decimal dice directory directorystructure do-not-use drawing dude dynamic error exec executable factorial fgets file fork framework free function functions givemetehcode givemetehcodez grade graphics gtkgcurlcompiling haiku hangman help|help|help|help homework i/o input insert int integer intmain() lazy line linked linkedlist linux list lists loop lowest malloc matrix memory mysql no-effort output parallel path permutations pointer pointers problem process profile program programming read recursion recursive recv reverse scanf segmentationfault socketprograming spoonfeeding stack string strings strtok structures student system testing turbo-c turboc unix user variable windows _getdelim






