思路
先用getline()函数读入string类型的字符串s1,s2.然后用rfind()查找函数进行查找
代码
#include<iostream>
#include<string>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s1,s2;
getline(cin, s1);
getline(cin, s2);
if (s2.rfind(s1)!=string::npos)
{
cout <<"index = "<< s2.rfind(s1) << endl;
}
else
{
cout << "Not Found" << endl;
}
return 0;
}
输入和结果
am
I am a teacher
index = 2