Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,348 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,145 people online right now. Registration is fast and FREE... Join Now!




WordCount in string help

 
Reply to this topicStart new topic

WordCount in string help

zerogee
post 21 Aug, 2008 - 03:28 AM
Post #1


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 42

This may sound stupid, but for the life of me I cannot find the correct code/format for WordCount. I need to count the number of words in a string. And what STL is it listed in. I have tried this code but it did not work.
CODE
WordCount (stringname)
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 21 Aug, 2008 - 03:33 AM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Make your own word count function?
It's simple enough, all you need to do is loop through a string and count the spaces.

You might want to account for double spaces, too.

If you check the snippets, there are most likely some gems to be found in there smile.gif
User is offlineProfile CardPM

Go to the top of the page

flumer
post 21 Aug, 2008 - 07:36 PM
Post #3


New D.I.C Head

*
Joined: 8 Jun, 2007
Posts: 5


My Contributions


Actually
if you have used a command named grep in Linux,you can do wordcount very simplely.
CODE

/*don't count number
make number as space
between words can have two space
*/
//I CAN'T WARRANT THIS FUNCTION IS RIGHT
#include<ctype.h>
int wordcount(char *string){
    int count;
    while(*string!='\0'){
        if(isalpha(string)){
            count++;
            while(*string!=' '){
                if(*string=='\0')
                return count;
            }
        }
    }
}

RECOMMAND USING LINUX
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 21 Aug, 2008 - 07:37 PM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(gabehabe @ 21 Aug, 2008 - 07:33 AM) *

It's simple enough, all you need to do is loop through a string and count the spaces.

You'll have to watch for back to back spaces, or else you'll get bogus results.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 22 Aug, 2008 - 03:15 AM
Post #5


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


QUOTE(no2pencil @ 22 Aug, 2008 - 04:37 AM) *

QUOTE(gabehabe @ 21 Aug, 2008 - 07:33 AM) *

It's simple enough, all you need to do is loop through a string and count the spaces.

You'll have to watch for back to back spaces, or else you'll get bogus results.


QUOTE(me)
You might want to account for double spaces, too.

smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 03:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month