找回密码
 中文实名注册
搜索
查看: 974|回复: 1

9815: 查找替换

[复制链接]

725

主题

584

回帖

3万

积分

管理员

积分
31222
发表于 2022-7-10 18:57:27 | 显示全部楼层 |阅读模式
题目描述
把母串中的要替换的的字符串,替换成新的串,注意是全部替换
输入
三行分别是原串,需要替换串,替换串
输出
被替换好的字符串


输入
dog is stupid dog is stupid
dog
pigs
输出
pigs is stupid pigs is stupid


回复

使用道具 举报

725

主题

584

回帖

3万

积分

管理员

积分
31222
 楼主| 发表于 2022-7-10 19:20:02 | 显示全部楼层
[C++] 纯文本查看 复制代码
#include<bits/stdc++.h>
using namespace std;
int  main()
{
    string str1,str2,str3;
    int n,m;
    
    getline(cin,str1);
    cin>>str2>>str3;
    
    //cout<<str1<<endl;
	//cout<<str2<<endl;
	//cout<<str3<<endl;

   n=str1.find(str2);
   while(n != -1) {
    
    str1.replace(n, str2.length(), str3);
    n=str1.find(str2);
	}
    
    cout<<str1<<endl;
    
    return 0;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

快速回复 返回顶部 返回列表