将文本文件中指定的字符串替换成新字符串。 由于目前的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. beyond compare 用法

    1.过滤器用法:点击小眼睛(回话-->回话设置)打开过滤器界面----设置多个过滤文件或目录直接使用回车键 2.比较时最好先设置编码以防出现乱码问题 工具-->文件格式. 3.比较时出现乱 ...

  2. 一个arctan积分的两种解法

    \[\Large\int_{0}^{1}\frac{\arctan x}{\sqrt{1-x^{2}}}\mathrm{d}x\] \(\Large\mathbf{Solution:}\) 首先第一种 ...

  3. HDU 4699 Editor(模拟 对顶栈)

    题目大意: 给定一个整数序列 维护5种操作 次数<1e6 I x: 光标位置插入x 然后光标位于x之后 D: 删除光标前一个数 L: 光标左移 R: 光标右移 Q k: 询问位置k之前的最大前缀 ...

  4. LNMP架构及应用部署!(重点)

    LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构. WNMP代表的就是:Windows系统下Nginx+MySQL+PHP这种网站服务器架构. WAMP代表的就是: ...

  5. windows下mysql 8.0.12安装步骤及基本使用教程

    本文实例为大家分享了windows下mysql 8.0.12安装步骤及使用教程,供大家参考,具体内容如下 补充:mysql 已经更新到了 8.0.19,大致步骤和这个差不多,照着来就完事了. 我下载的 ...

  6. MySQL - 设置UTF-8编码

    1. 在Windows上,安装时请选择UTF-8编码,以便正确地处理中文. 在Mac或Linux上,需要编辑MySQL的配置文件,把数据库默认的编码全部改为UTF-8.MySQL的配置文件默认存放在/ ...

  7. 神奇的 SQL 之 联表细节 → MySQL JOIN 的执行过程

    问题背景 对于 MySQL 的 JOIN,不知道大家有没有去想过他的执行流程,亦或有没有怀疑过自己的理解(自信满满的自我认为!):如果大家不知道怎么检验,可以试着回答如下的问题 驱动表的选择 MySQ ...

  8. 第六周之Hadoop学习(六)

    继续上周开启telnet的过程,这个过程发现win10上运行不了telnet的命令 原因大概在于没有开启telnet服务,从网上下载好telent服务端,安装后继续尝试是否能在win10上使用hado ...

  9. HTML5中改变了哪些东西?

    HTML5 推出的理由 想要把目前web上存在的各种问题一并解决 Web浏览器之间的兼容性很低 文档结构不够明确 Web应用程序的功能受到了限制 HTML5重新定义了浏览器的统一标准 HTML5 与 ...

  10. nginx 加工上游服务器返回的内容,并返回给客户端

    禁用上游响应头部功能 Syntax: proxy_ignore_headers field ...; Default: — Context: http, server, location 功能介绍:某 ...