http://noi.openjudge.cn/ch0107/21/
這裏需要對輸入進行控制,第一行輸入多個字符串後,以回車結束,使用了getchar()來存儲輸入的字符,並和換行符'\n'比較。
//http://noi.openjudge.cn/ch0107/21/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
string s[100], a, b;
char ch;
int i=0;
//一行輸入字符數組,遇到回車結束輸入
while(cin >> s[i])
{
i++;
if(ch=getchar()=='\n')
break;
}
cin >> a >> b;
for(int j=0; j<i; j++)
{
if(s[j]==a)
s[j]=b;
}
for(int j=0; j<i; j++)
{
cout << s[j] << " ";
}
return 0;
}