RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 831 | Replies: 1 | Thread Tools  Display Modes
Reply
Join Date: Sep 2005
Posts: 1
Reputation: haruhiko is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
haruhiko haruhiko is offline Offline
Newbie Poster

Solution Need Help Please.....

  #1  
Sep 9th, 2005
Hi, I'm a newbie here. I just want to know how to change this code so it can count word instead of letter

code:
#include <iostream>
#include <fstream>

using namespace std;

// count letters 'a' to 'z' in string s
void countLetters( string s )
{
        int pos, sum = 0;
        char m;

        // only a to z
        for( m = 97; m < 123; m++ )
        {
                // start with index = 0
                pos = s.find( m, 0 );
                while( pos != string::npos )
                {
                        // adjust index up
                        pos = s.find( m, pos+1 );
                        // total up this character
                        sum++;
                }
                if ( sum > 0)
                {
                        cout << m << " = " << sum << endl;
                }
                sum = 0;
        }
}

int main()
{
        char c;
        string str1,word,ch;
        ifstream is;

        is.open("input.txt");
        
      //   loop while extraction from file is possible
        while ( is.good() ) {
                // get character from file
                c = is.get();
                cout << c;
                // build string with all lower case characters
                c = tolower( c );
                str1.push_back( c );
        }
        cout << endl << endl;
        cout << str1 << endl;
        // count letters a to z
        countLetters(str1);

        // close file
        is.close();

        cin.get(); // wait
        return 0;
}
<< moderator edit: added [code][/code] tags >>


Really appreciate if somone can help me. Thanks....
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Need Help Please.....

  #2  
Sep 9th, 2005
Here's a hint:

Words are defined by spaces. Ergo, count spaces, and you can count words.

Here's another hint. (using underscores _ to represent spaces)
What's the difference between "It_is" and "It______is"?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C++ Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 7:09 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC