将文本文件中指定的字符串替换成新字符串。 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束。end后面有两个字符串,要求用第二个字符串替换文本中所有的第一个字符串。

输入格式:

Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology. The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

end (表示结束)

Institute (第一个字符串,要求用第二个字符串替换)

University (第二个字符串)

输出格式:

Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

输入样例:

Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.

The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

end

Institute

University

输出样例:

Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

解题:利用find函数和replace函数

find函数
1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回-1
2.查找某一给定位置后的子串的位置。例如str.find("bad",5),从母串下标为5开始搜索bad的位置,如果存在返回bad在母串的下标否则返回-1
3.查找所有子串在母串中出现的位置(此题就是如此)
模板:
int found=str.find("bad",0);

while(found!=-1)
{
cout<<found<<endl;//输出bad在母串的位置
found=str.find("bad",found+1);
}
4.反向查找子串在母串中出现的位置,通常我们可以这样来使用,当正向查找与反向查找得到的位置不相同说明子串不唯一。
str.rfind("bad");
replace函数
replace最常用的形式是str.replace(found,length,c)//在字符串str中从found位置开始用字符串c替换总长为length的字符串

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string a,b,c,m;
getline(cin,a);
while()
{
getline(cin,m);
if(m=="end"){
break;
}
a+='\n';
a+=m;
}
a+='\n';
getline(cin,b);
getline(cin,c);
int found;
found=a.find(b);
while(found!=-)
{
a.replace(found,b.size(),c);
found=a.find(b,found+);
}cout<<a;
return ;
} 

字符串替换 (replace)的更多相关文章

  1. 字符串替换replace方法

    字符串替换replace方法: http://www.w3school.com.cn/jsref/jsref_replace.asp http://www.cnblogs.com/skywang/ar ...

  2. Python3字符串替换replace(),translate(),re.sub()

    Python3的字符串替换,这里总结了三个函数,replace()和translate()和re.sub() replace() replace() 方法把字符串中的 old(旧字符串) 替换成 ne ...

  3. C#不区分大小写的字符串替换(Replace)函数

    在.NET中,不调用C++/CLI,进行字符串替换有好几种方法: 1.最常用的,就是String实例.Replace(),但这个不能忽略大小写. 2.System.Text.Regex(Regular ...

  4. oracle学习笔记:字符串替换 replace、regexp_replace、translate函数

    1.replace 函数 语法:replace(char, search_string, replacement_string) --针对字符串替换 功能: ​ 将char中的字符串替换. ​ 当re ...

  5. C# 字符串替换Replace

    C# 中的石strA.Replace(strB,strC)函数可以实现将strA中的strB替换为strC. 但是容易出错的地方是,这并不是就直接替换好了,此函数的返回值才是替换好的字符串,所以还要要 ...

  6. JQuery字符串替换replace方法

    在日常的js开发中,常常会用到JQuery, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replac ...

  7. C#自定义字符串替换Replace方法

    前一阵遇到一个如标题的算法题,是将原有字符串的某些片段替换成指定的新字符串片段,例如将源字符串:abcdeabcdfbcdefg中的cde替换成12345,得到结果字符串:ab12345abcdfb1 ...

  8. JavaScript字符串替换replace方法

    在日常的js开发中, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replace('aa','dd') ...

  9. Java之字符串替换replace()

    replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的 import java.uti ...

  10. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

随机推荐

  1. python opencv:使用滑动条做调色板

    cv2.getTrackbarPos() 函数的 一个参数是滑动条的名字, 第二个参数是滑动条被放置窗口的名字, 第三个参数是滑动条的默认位置. 第四个参数是滑动条的最大值, 第五个函数是回调函数,每 ...

  2. Linux 笔记:虚拟控制台

    登录后按Alt+F2键这时又可以看到"login:"提示符,这个就是第二个虚拟控制台. 一般新安装的Linux有四个虚拟控制台,可以用Alt+F1~Alt+F4来访问. 虚拟控制台 ...

  3. 创建mysql数据库,在新数据库中创建表,再尝试删除表

    创建之前,先登录数据库存 mysql -u 账号 -p密码 登录完成后,展示一下已存在的数据库 show databases; 创建数据库 create database test111; 然后展示一 ...

  4. MQTT.js browser node 均支持

    npm - mqtt 官网手册 https://www.npmjs.com/package/mqtt#weapp 简书用户 使用笔记 https://www.jianshu.com/p/4fd95ca ...

  5. 【原】从浏览器数据一个URL的全过程

    1.根据域名到DNS找到IP 2.根据IP建立TCP三次握手连接 3.连接成功发出http请求 4.服务器响应http请求 5.浏览器解析html代码并请求html中的静态资源(js/css) 6.关 ...

  6. 洛谷P2142 高精度减法 题解

    想找原题请点击这里:传送门 原题: 题目描述 高精度减法 输入格式 两个整数a,b(第二个可能比第一个大) 输出格式 结果(是负数要输出负号) 输入输出样例 输入 复制 输出 复制 说明/提示 %数据 ...

  7. Pycharm 在 import requests 时报错

    requests的安装我尝试了两种方法: 第一种:下载安装 https://github.com/requests/requests 然后解压到:(python的安装路径,这里使用的是默认路径) Ap ...

  8. 计算机网络 - TCP/IP模型

    图片来自网上资料

  9. 操作系统OS - Zip Bomb

    42.zip - A 42 kb zip file that contains 4.5 Petabytes of uncompressed data.

  10. hdoj6703 2019 CCPC网络选拔赛 1002 array

    题意 description You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of the ...