字符串替换

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/archive/2012/07/08/2581731.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/54657

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午5:50
下一篇 2023年2月9日 上午5:50

相关推荐