字符串替换 (replace)
输入格式:
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)的更多相关文章
- 字符串替换replace方法
字符串替换replace方法: http://www.w3school.com.cn/jsref/jsref_replace.asp http://www.cnblogs.com/skywang/ar ...
- Python3字符串替换replace(),translate(),re.sub()
Python3的字符串替换,这里总结了三个函数,replace()和translate()和re.sub() replace() replace() 方法把字符串中的 old(旧字符串) 替换成 ne ...
- C#不区分大小写的字符串替换(Replace)函数
在.NET中,不调用C++/CLI,进行字符串替换有好几种方法: 1.最常用的,就是String实例.Replace(),但这个不能忽略大小写. 2.System.Text.Regex(Regular ...
- oracle学习笔记:字符串替换 replace、regexp_replace、translate函数
1.replace 函数 语法:replace(char, search_string, replacement_string) --针对字符串替换 功能: 将char中的字符串替换. 当re ...
- C# 字符串替换Replace
C# 中的石strA.Replace(strB,strC)函数可以实现将strA中的strB替换为strC. 但是容易出错的地方是,这并不是就直接替换好了,此函数的返回值才是替换好的字符串,所以还要要 ...
- JQuery字符串替换replace方法
在日常的js开发中,常常会用到JQuery, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replac ...
- C#自定义字符串替换Replace方法
前一阵遇到一个如标题的算法题,是将原有字符串的某些片段替换成指定的新字符串片段,例如将源字符串:abcdeabcdfbcdefg中的cde替换成12345,得到结果字符串:ab12345abcdfb1 ...
- JavaScript字符串替换replace方法
在日常的js开发中, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replace('aa','dd') ...
- Java之字符串替换replace()
replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的 import java.uti ...
- 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 ...
随机推荐
- java编码格式大讲解
oracle 分页: -- 第一种 select * from (select aed.*, row_number() over(order by aed.created_date) rw from ...
- 线上学习-语言模型 language model
chain rule markov assumption 评估语言模型 平滑方法
- dp求解各种子串子序列
目录 概念 最长上升子序列 最长连续子串 最长公共子序列 最长公共上升子序列 注:dp可能并不是求解该这些问题的最优算法,这里只是做一个dp 算法的简介. 概念 定义:假设现有一个 string = ...
- iOS 开发之 FMDB 源码分析
概念: FMDB 是用于数据存储的框架,它是 iOS 平台下对 SQLite 数据库的封装.FMDB 是面向对象的,它以 OC 的方式封装了 SQLite 的 C 语言 API,使用起来更加方便. C ...
- Fedora26安装Mysql实记(包含yum换源教程)
课程实验中有一项是安装Hive,这个过程中需要安装Mysql. 在安装Mysql耗费了好多时间,总是报错数据仓库同步失败什么的. 输入yum install mysql 就提示错误,连接不上数据源.该 ...
- 解决CentOS下boost安装后不能使用的问题
先说一说整个经历. 因为之前没有注意到gcc4.8.5比较旧,就已经安装好boost了,当时已经可以使用了,后来发现gcc太老了,一些软件安装需要比较新的gcc支持,所以决定升级gcc,结果boost ...
- python闯关之路一(语法基础)
1,什么是编程?为什么要编程? 答:编程是个动词,编程就等于写代码,那么写代码是为了什么呢?也就是为什么要编程呢,肯定是为了让计算机帮我们搞事情,代码就是计算机能理解的语言. 2,编程语言进化史是 ...
- 【Javaweb】Servlet的xml和注解配置
1.xml <%@ page language="java" contentType="text/html;" %> <!DOCTYPE ht ...
- 概念 - 抖脚(Fidgeting)
参考 https://cn.nytimes.com/health/20161220/why-fidgeting-is-good-medicine/dual/ https://baike.baidu.c ...
- idea激活,使用破解补丁无需注册码
idea激活,使用破解补丁无需注册码 2017年08月19日 10:57:54 标签: idea / 破解 / 补丁 / 软件 / 13891 编辑 删除 idea激活,JetBrain旗下软件激活 ...