Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s; // 读取输入的字符串
string toReplace = "black";
string replacement = "ikun";
size_t pos = 0; // 初始化查找位置
while ((pos = s.find(toReplace, pos)) != string::npos) {
// 当找到"black"时,使用substr和+=来替换它
s = s.substr(0, pos) + replacement + s.substr(pos + toReplace.length());
// 更新查找位置,跳过已经替换的部分
pos += replacement.length();
}
cout << s << endl; // 输出修改后的字符串
return 0;
}
AC: