思路
因为直接使用 find 的话不是单词也可能匹配到,所以在 a,b 前面加了空格,主要使用了 C++ 库函数的 erase(pos, len),清楚 pos 开始的长度为 len 的子串,insert(pos, b) 在 pos 位置插入字符串 b
#include <iostream>
#include <string>
using namespace std;
int main(){
string s,a,b;
getline(cin,s);
getline(cin,a);
getline(cin,b);
s = " " + s + " ";
a = " " + a + " ";
b = " " + b + " ";
int start;
while(1){
start=s.find(a);
if(start == string::npos)
break;
else{
s.erase(start,a.length());
s.insert(start,b);
}
}
int n = s.size();
cout<<s.substr(1, n - 2);
return 0;
} 
京公网安备 11010502036488号