字符串替换 (replace)

将文本文件中指定的字符串替换成新字符串。 由于目前的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.
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string a,b,c,m;
getline(cin,a);
while(1)
{
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!=-1)
{
a.replace(found,b.size(),c);
found=a.find(b,found+1);
}cout<<a;
return 0;
}

  

原文地址:https://www.cnblogs.com/cstdio1/p/11115825.html

时间: 2024-08-28 01:29:32

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

C#自定义字符串替换Replace方法实例

本文实例讲述了C#自定义字符串替换Replace方法.分享给大家供大家参考.具体实现方法如下: 一.问题: 前一阵遇到一个如标题的算法题,是将原有字符串的某些片段替换成指定的新字符串片段,例如将源字符串:abcdeabcdfbcdefg中的cde替换成12345,得到结果字符串:ab12345abcdfb12345fg,即:abcdeabcdfbcdefg -> ab12345abcdfb12345fg. 二.实现方法: 显然不能用string.Replace方法,需要自定义一个方法 strin

js字符串替换(replace)

记录一个开发中所犯的错误. 需求:用js将字符串中的某些子字符串替换为指定的新字符串. 实现思路:印象中js字符串替换有replace方法,replace方法接收两个参数,第一个为要替换的子字符串或正则匹配模式,第二个参数为新字符串.自己对正则不熟,认为用字符串能满足需求. 简单测试 var str="apples are round"; var newStr = str.replace('apples','oranges') //newStr 值为:oranges are round

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

Python3的字符串替换,这里总结了三个函数,replace()和translate()和re.sub() replace() replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次 str.replace(old, new[, max]) a = 'Hello,world. ByeBye!' print(a.replace('l','Q')) print(a.replace('abcdefghi','01234

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

前一阵遇到一个如标题的算法题,是将原有字符串的某些片段替换成指定的新字符串片段,例如将源字符串:abcdeabcdfbcdefg中的cde替换成12345,得到结果字符串:ab12345abcdfb12345fg,即:abcdeabcdfbcdefg -> ab12345abcdfb12345fg. 显然不能用string.Replace方法,需要自定义一个方法 string Replace(string originalString, string strToBeReplaced, strin

nyoj 113 字符串替换 (string中替换函数replace()和查找函数find())

字符串替换 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 编写一个程序实现将字符串中的所有"you"替换成"we" 输入 输入包含多行数据 每行数据是一个字符串,长度不超过1000 数据以EOF结束 输出 对于输入的每一行,输出替换后的字符串 样例输入 you are what you do 样例输出 we are what we do读一行的方法:用geiline(cin,s) 1 #include <iostream>

winform学习日志(三十)----------从字符串总分离文件路径、命名、扩展名,Substring(),LastIndexOf()的使用;替换某一类字符串,Replace()的用法

一:从字符串总分离文件路径.命名.扩展名,上图 二:代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace FilePathString { public par

POJ1572 Automatic Editing 字符串替换,replace就够了

题意不难理解,但是一开始还是没有看清楚题目.Replace the first occurrence of the find string within the text by the replace-by string, then try to perform the same replacement again on the new text. Continue until the find string no longer occurs within the text, and then

Java字符串替换函数replace、replaceFirst、replaceAll

一.replace(String old,String new) 功能:将字符串中的所有old子字符串替换成new字符串 示例 String s="Hollow world!"; System.out.println(s); System.out.println(s.replace("o", "#")); /* * 结果:Hollow world! * H#ll#w w#rld! */ 二.replaceAll(String arg0, Stri

python 字符串替换功能 string.replace()可以用正则表达式,更优雅

说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变得优雅简洁: 备注:上图中的base_info 是 pandas 里的 dataframe 数据结构,可以用上述方法使用 string 的 replace 方法. 原文地址:https://www.cnblogs.com/jjliu/p/11514226.html