2019/8/6
//将"abcdeababef"中的"ab"替换成"c";
#include <iostream>
#include <string>
using namespace std;
void replace_ab(string& str)
{
int pos = 0;
while ((pos = str.find("ab", pos) )!= string::npos)
{
//第一,一定要注意运算符的优先级,加括号
str.replace(pos,2,"c");
//pos加的是替换后的长度
pos += 1;
}
}
int main()
{
string s = "abcab";
replace_ab(s);
cout << s << endl;
string ss = "qwerty";
replace_ab(ss);
cout << ss << endl;
string sn = "ababdfabhunabmjbbbababababbb";
replace_ab(sn);
cout << sn << endl;
system("pause");
return 0;
}
#笔试题目#