//直接将句子分割成一个个单词 //然后将每个单词与待替换单词匹配 //若不相同直接输出单词,否则输出替换后单词 #include <iostream> #include <string> #include <vector> using namespace std; int main() { string str, str1, str2; char c; vector<string>data; while ((cin >> str).get(c)) { data.push_back(str); if (c == '\n') { //检测到回车后结束输入 break; } } cin >> str1 >> str2; for (auto iter = data.begin(); iter != data.end(); iter++) { if (*iter != str1) { cout << *iter << " "; } else { cout << str2 << " "; } } return 0; }