0
已解决
1466 单词替换
经验值:0 时间限制:1000毫秒
题目描述 Description
在Word办公软件当中,经常会用到单词替换这个功能,即先找到需要替换的单词,然后用新单词将其替换掉。现有一个字符串,字符串中有很多单词,每个单词之间用空格隔开,指定需要替换的单词,并给定新的单词将其替换掉,输出替换过后的字符串(如果原串中没有指定的单词,则输出原串)。
输入描述 Input Description
输入为2行:
第一行为原字符串(长度不超过1000)。
第二行为2个单词,第一个单词表示指定的需要替换的单词,第二个为给定的将其替换的新单词(每个单词长度不超过100),中间有空格隔开。
输出描述 Output Description
输出为一行,为替换后的新字符串。
样例输入 Sample Input
I like my students like love
样例输出 Sample Output
I love my students
20分:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a;
bool f=0;
string s,b,c,m;
getline(cin,s);
cin>>b>>c;
m=s;
if(s.find(b,0)!=-1){
f=1;
a=s.find(b,0);
s.erase(a,b.size());
s.insert(a,c);
}
else f=0;
if(f) cout<<s;
else cout<<m;
return 0;
}