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