2

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

Mike
  • 47,263
  • 29
  • 113
  • 177
Tomáš Ptáček
  • 53
  • 1
  • 1
  • 6
  • Please tag homework as such and include the code right in your question without linking to pastebin. If you indent a line by 4 spaces it gets automatically formated as code with a monospace font. – PeterT Nov 26 '12 at 13:35
  • 3
    @PeterT The homework tag should not be used anymore on SO. – Nikos C. Nov 26 '12 at 13:41
  • possible duplicate of [Find all a substring's occurrences and locations](http://stackoverflow.com/questions/4034750/find-all-a-substrings-occurrences-and-locations) – jcoder Nov 26 '12 at 14:54
  • 1
    @janisz - Please don't add the homework tag, it's obsolete – Mike Nov 26 '12 at 15:13

4 Answers4

2

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;
 }
janisz
  • 6,292
  • 4
  • 37
  • 70
1

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.

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
0
#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;
        }``


}
Rabiya
  • 1
  • 1
0

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

rashedcs
  • 3,588
  • 2
  • 39
  • 40