字符串替换


  
  
   
   描述
  
  
  
  
输入一个字符串,以回车结束(字符串长度<=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;


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值