Question #35734

a program which find occurence of words in a string . for example i play cricket. cricket is national game of england. and cricket is played all over the world
output should be cricket = 3
Note : please help me in basic program not complicated
1

Expert's answer

2013-10-14T08:34:26-0400
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
bool search(string &s, string word)
{
    transform(word.begin(), word.end(), word.begin(), ::tolower);
    transform(s.begin(), s.end(), s.begin(), ::tolower);
    if(s.find(word) != std::string::npos)
        return true;
    return false;
}
int main(int argc, char* argv[])
{
    string sWord, InputString;
    cout << "Input word:"; getline(cin, sWord);
    cout << "Input text:"; getline(cin, InputString);
    istringstream ss(InputString);
    string word;
    unsigned int iCountWord = 0;
    while(ss >> word)
    {
        if(search(word, sWord))
            iCountWord++;
    }
    cout << sWord << " = " << iCountWord << endl;
    cin.get();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS