NYOJ-113-字符串替换

原题地址

字符串替换

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

每行数据是一个字符串,长度不超过1000 
数据以EOF结束
输出
对于输入的每一行,输出替换后的字符串
样例输入
you are what you do
样例输出
we are what we do
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string.h>
 4 using namespace std;
 5 
 6 #define s1 "you"
 7 #define s2 "we"
 8 
 9 int main()
10 {
11     string s;
12     int sign;
13     while(getline(cin,s))               //整行输入
14     {
15         sign=s.find(s1,0);              //
16         while(sign!=string::npos)       //没到末尾时
17         {
18             s.replace(sign,3,s2);       //找到的话将匹配的3个字符替换为s2
19             sign=s.find(s1,sign+1);
20         }
21         cout<<s<<endl;
22     }
23     return 0;
24 }
原文地址:https://www.cnblogs.com/qq1353842241/p/8017241.html