题解 | 单词替换
单词替换
https://www.nowcoder.com/practice/5b58a04679d5419caf62c2b238e5c9c7
#include <iostream>
#include <cstring>
#include <sstream>
using namespace std;
const int N = 100 + 10;
stringstream ss;
string s;
string a, b;
string word[N];
string ans;
int main() {
while (getline(cin, s)) {
getline(cin, a);
getline(cin, b);
ss.str(s);
int n = 0;
while (ss >> word[n]) n++;
for (int i = 0; i < n; i++) {
if (word[i] == a) ans += b + ' ';
else ans += word[i] + ' ';
}
cout << ans << endl;
}
}
stringstream把单词拆分到数组中,然后逐个加到答案即可。

