Home > Enterprise >  How can I count the number of times that a word appears in a string?
How can I count the number of times that a word appears in a string?

Time:04-04

#include <iostream>
#include <string>

using namespace std;

int main() {

    string userString;
    string userWord;
    string newString;
    char userChar;
    int wordCount = 0, pos;


    cout << "Enter s for search, r for replace, or q to quit:" << endl;

    cin >> userChar;

    while(userChar != 'q') {

        cout << userChar << endl;
        if(userChar == 's') {
            cout << userChar << endl;

            cout << "Enter a paragraph:" << endl;

            cin.ignore(2);//have to use 2 for some reason or outputting will be messed up. 

            getline(cin, userString);

            cout << userString << endl;

            cout << "Enter a word to search" << endl;

            cin >> userWord;

            pos = userString.find(userWord); //idx of where userWord starts 
            
           while(userString.find(userWord) != string::npos) {
                // not sure what put here to find the word  
                wordCount  ;


            }
            cout << "Occurences of Word = " << wordCount << endl;
            cout << "Enter s for search, r for replace or q to quit:" << endl;
            cin >> userChar;
        }
        return 0;
    }



I am trying to figure out what to put in the while loop : while (userString.find(userWord) != string::npos) that will search for the word in the user-provided paragraph.

Have not learned arrays yet and can't use vectors both of which were not taught either. Only way I can think of is using string functions since this is an entry course and the only way we have been taught to manipulate strings.

Also I am a bit unsure on what cin.ignore() does. I just know I have to use it twice (cin.ignore(2)) or otherwise the output gets butchered when I run the program.

Testing format: userChar //can be s(for search), r(for replace), q(for quit) userString //the paragraph or sentence userWord // word to look for in that paragraph q // to quit and avoid a infinite loop

Here is what I tested with:

s Today is sunny and tomorrow will also be sunny. sunny q

In the code I am unsure of how to search for the word.

CodePudding user response:

Your main issue here:

pos = userString.find(userWord); //idx of where userWord starts 
while(userString.find(userWord) != string::npos) {
    // not sure what put here to find the word  
    wordCount  ;
}

You are not setting pos to the next value. std::string::find takes an offset into the string where to search and returns the position in the string where the start of the searched-for string was found. You need to advance the offset or it will keep "finding" the same word. The simplest is to advance it by one to the next character; but, it's more efficient to advance it by the length of the searched-for string:

std::size_t pos = userString.find(userWord); 
while(pos != std::string::npos) {
    wordCount  ;
    pos = userString.find(userWord, pos   userWord.size();
}
std::cout << "Occurences of Word = " << wordCount << std::endl;

CodePudding user response:

you can also this approach

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[200], word[20];
    int i=0, j, temp, countW=0, chk;
    cout<<"Enter the String: ";
    gets(str);
    cout<<"Enter the Word: ";
    gets(word);
    while(str[i]!='\0')
    {
        temp = i;
        j=0;
        while(word[j]!='\0')
        {
            if(str[i]==word[j])
                i  ;
            j  ;
        }
        chk = i-temp;
        if(chk==j)
            countW  ;
        i = temp;
        i  ;
    }
    cout<<"\nOccurrence of '"<<word<<"' = "<<countW;
    cout<<endl;
    return 0;
}
  • Related