Description
fjxmlhx hates marshtomp, so he wants to ignore the "marshtomp" in every sentences. In order to making the sentence complete, he changes the word "marshtomp" into "fjxmlhx"
please create a class, then you must solve the problem via calling the method of the class with the object of the class.
Input
the input contains several lines
each line there exists a string whose length is no more than 200
Attention: the end of line has nothing to do with the beginning of the next line
Output
print the answer of the problem
Sample Input
marshTomp is beaten by fjxmlhx! AmarshtompB
Sample Output
fjxmlhx is beaten by fjxmlhx! AfjxmlhxB
---------------------------------------------------------------------------------------------------------------------------------
(ps:zcmuoj的4925,1655也是字符串处理题,其中有大小写区分的要注意再建立一组字符串记录删除位置)
本题主要涉及c++的函数:find,npos,size(),begin(),tolower
贴上我学习过的链接:
(14条消息) C++ replace() 函数用法_cai_niaocainiao的博客-CSDN博客_c++ replace
C++ STL set容器完全攻略(超级详细) (biancheng.net)
做了两道字符串增删类的题我"举二反一"一下~
读完题目后,观察sample可以发现被删去的字符串中的字母t具有大小写区分。我们可以建立第二个字符串(该字符串为需要处理的字符串的小写版本)帮助记录需要删去的字符的初始位置。统一转换为小写之后便于比对。
ac代码↓
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i;
string s,t;
string hate="marshtomp",love="fjxmlhx";//注意marshtomp中t的大小写有区分
while(getline(cin,s))
{
t=s;//将s复制到t(下面会利用到s与t的一一对应关系)
int len=hate.size();
for(i=0;i<s.size();i++)
{
t[i]=tolower(t[i]);//将t全部转化为小写
}
int p=t.find(hate);//找到被替换的字符串的初始位置
while(p!=string::npos)//直到处理到输入字符串的末尾
{
t.replace(t.begin()+p,t.begin()+p+len,love);
s.replace(s.begin()+p,s.begin()+p+len,love);
p=t.find(hate);//p用于记录被删去字符串的初始位置
}
cout<<s<<endl;
}
}