Hi there!
I'd like to check if a string is preceeded by another one.
More precisely in this example i would like to check if there is "lion" right before "sleeps".
Im using rfind() to search backwards but for some reason it starts the search from the
beginning of the line and not from "pos" as i liked to that it does.
Should i use another search function maybe?
Thanks in advance!
#include <string>
#include <iostream>
using namespace std;
int main () {
string str = "The lion aaaaaaa sleeps tonight";
string str2 = "lion";
string str3 = "sleeps";
string::size_type loc = 0;
string::size_type pos = 0;
if (( pos = str.find(str3, pos)) != string::npos)
{
cout<<"str3 is at position: "<< pos <<endl;
}
loc = str.rfind ( str2.c_str(), pos);
if (loc != string::npos)
{
cout << "Match found at position: " << loc << endl;
}
else
cout << "Match not found." << endl;
system("pause");
return 0;
}