C++ Find word in sentence

I've got a task to find the word "EYE" in a sentence (more like just line of chars) such as: EYEYECARASDFG. As you can see, the word "EYE" is there twice, overlapping each other. I am suppose to cout how many times the word "EYE" occurs in the sentence. I wrote some code that looks like this:

#include <iostream>
#include <string>

using namespace std;

string sentence;

int main()
{
    int i = 0;
    cin >> sentence;

    while()
    {
        if (std::string::npos != sentence.find("EYE"))
        {
            i++;
        }
    }


    cout << i;
}

Now without the while loop, it finds the EYE in the sentence and it kinda works. So I though, to count with the overlapping and make the code running until it hits the end, I need to loop it. So I though the while loop would be the best, but I don't know how to loop it, what to put into the brackets for while loop

Upvotes: 2

Views: 41207

Answers (4)

rashedcs
rashedcs

Reputation: 3725

Code Snippet :

#include <bits/stdc++.h>
using namespace std;



int main()
{
    string input, word;
    getline(cin, input);
    cin>>word;

    int cnt=0;
    size_t pos = input.find(word, 0);
    while(pos != string::npos)
    {
        cnt++;
        pos = input.find(word, pos+1);
    }
    cout<<cnt<<endl;

    return 0;
}

Input :
Python Programming Python
Python

Output : 2

Upvotes: 0

Rabiya
Rabiya

Reputation: 1

#include<iostream>
#include<string>
using namespace std;
main()
{
        string sen, sub;
        int pos;
        cout<<"Enter the Sentence"<<endl;
        getline(cin,sen);
        cout<<"Enter string to find"<<e`ndl;
        cin>>sub;
        for (int i=1;(pos=sen.find(sub)) != -1 ;i++)
        {
                sen=sen.substr(++pos);
                cout<<"Found = "<<sub<<" "<<i<<" Times"<<endl;
        }``


}

Upvotes: 0

janisz
janisz

Reputation: 6356

First of all condition in while is required. If you want infinite loop use true as your statement. As a first draft, try to make it with "brute force". Just check every 3 letters substring of your sentence if equals "EYE". It will be one loop and 3 conditions or 2 loops and 1 condition. Then read about some text search algorithm e.g KMP.

If you just want to make this code run use following coed:

int pos = 0;
while(true) { 
    pos =  sentence.find("EYE", ++pos);
    if (pos != std::string::npos) {
        i++;
    } else break;
 }

Upvotes: 2

Jive Dadson
Jive Dadson

Reputation: 17016

You can do this using a finite-state machine. (Google it.) That is efficient and easy to understand. As you read the characters, there are three states to distinguish, i.e., 1) when the most recent letter seen was an "E", 2) when the last two seen were "EY" in that order, and 3) everything else. As you go through a character at at time, increase the "found"-count by one whenever you are in state 2 and find another "E".

See if you can take it from there with no more hints.

The idea can be extended to arbitrary strings besides "EYE", and you can write a compiler of sorts to generate the finite-state machines for those strings. But that is a more advanced assignment.

Upvotes: 2

Related Questions