字符串的替换

字符串替换

时间限制:3000 ms  |  内存限制:65535 KB
难度:2
 
描述
编写一个程序实现将字符串中的所有"you"替换成"we"
 
输入
输入包含多行数据 

每行数据是一个字符串,长度不超过1000 
数据以“#”结束
输出
对于输入的每一行,输出替换后的字符串
样例输入
you are what you do
样例输出
we are what we do






#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main()
{

int n;
cout<<"请输入实验次数"<<endl;
cin>>n;
while(n--)
{
string s=" ",s1;
cout<<"这是第"<<n+1<<"次"<<endl;
cin>>s1;
while(s1!="#") //字符串以"#"键结束
{
cout<<"s1== "<<s1<<endl;
s=s+" "+s1;
cout<<"s== "<<s<<endl;
cin>>s1;
}
cout<<"原版字符串 "<<s<<endl;
int i=0;
while(s.find("you",i)!=string::npos)    
{
i=s.find("you",i);             //找到单词you 的地方
s.replace(i,3,"we");         //将you 替换成we
}
cout<<"改进过的字符串"<<s<<endl;

}
return 0;
}



原文地址:https://www.cnblogs.com/zdblog/p/3637609.html