描述
输入一个字符串,以回车结束(字符串长度<=100)。该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写。现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符串。
输入
输入包括3行,
第1行是包含多个单词的字符串 s,
第2行是待替换的单词a,(长度<=100)
第3行是a将被替换的单词b。(长度<=100)
s, a, b 最前面和最后面都没有空格.
输出
输出只有 1 行,
将s中所有单词a替换成b之后的字符串。
样例输入
You want someone to help you
You
I
样例输出
I want someone to help you
#include<iostream>
#include<cstring>
#include <stdio.h>
using namespace std;
int main(){
int i,j,index,len,len2;
char str[101],word[101],newword[101],tmp[101];
while(gets(str)){
int index=0;
len=strlen(str);
cin>>word;
cin>>newword;
for(i=0;i<=len;i++){
if(str[i]==' '||i==len){
tmp[index]='\0';
if(strcmp(tmp,word)==0){
cout<<newword;
}
else{
cout<<tmp;
}
if(str[i]==' '){
cout<<" ";
}
else{
cout<<endl;
}
index=0;
memset(tmp,0,sizeof(tmp));
}
else{
tmp[index++]=str[i];
}
}
}
cin.get();
return 0;
}