字符串替换

Time Limit: 1000MS  Memory Limit: 65536K 

Description

编写一个C程序实现将字符串中的所有"you"替换成"we"

Input

输入包含多行数据 每行数据是一个字符串,长度不超过1000

数据以EOF结束

Output

对于输入的每一行,输出替换后的字符串

Sample Input

you are what you do

Sample

Output we are what we do

#include <iostream>  
#include <string> //c++中字符串操作的头文件!   
  
using namespace std;    
int main()  
{  
    string str;  
    while(getline(cin, str)) //getlin 函数   
    {  
        int start = str.find("you"); //找相应字符串位置的函数   
        while(start != string::npos) //这里要注意  字符串结尾标志:string::npos
        {  
            str.replace(start, 3, "we"); //替换字符串的函数   
            start = str.find("you", start+2); 
        }  
        cout << str << endl;  
    }   
    return 0;  
}  
原文地址:https://www.cnblogs.com/IThaitian/p/2581731.html