you->we

编写一个C程序实现将字符串中的所有"you"替换成"we"
Input
输入包含多行数据

每行数据是一个字符串,长度不超过1000
数据以EOF结束
Output
对于输入的每一行,输出替换后的字符串
Sample Input
you are what you do
Sample Output
we are what we do

 1 #include <string>  
 2 #include <iostream> 
 3 using namespace std; 
 4 int main() 
 5 { 
 6     string str; 
 7     while(getline(cin,str)) 
 8     { 
 9         string::size_type pos = 0; 
10         while ( (pos = str.find("you", pos)) != string::npos ) { 
11             str.replace( pos++, 3, "we" ); //3表示被替换字符子串长度
12         } 
13         cout << str << endl; 
14     } 
15     return 0; 
16 }

posted @ 2017-05-25 16:11  Legendary-X  阅读(333)  评论(0编辑  收藏  举报