fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽所有句子中的沼跃鱼(“marshtomp”,不区分大小写)。为了使句子不缺少成分,统一换成 “fjxmlhx” 。
输入
输入包括多行。
每行是一个字符串,长度不超过200。
一行的末尾与下一行的开头没有关系。
输出
输出包含多行,为输入按照描述中变换的结果。
样例输入
The Marshtomp has seen it all before. marshTomp is beaten by fjxmlhx! AmarshtompB
样例输出
The fjxmlhx has seen it all before. fjxmlhx is beaten by fjxmlhx! AfjxmlhxB
传统的字符串替换问题,问题是字符串由大小写转换成小写的时候,怎么转换回来,这里是用的两个字符串,一个字符串存大小写字符串,一个保存小写字符串。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std ;
int main()
{
string str ;
string des = "marshtomp" ;
string targ = "fjxmlhx";
while (getline(cin,str))
{
string temp = str ;
transform(temp.begin(),temp.end(),temp.begin(),::tolower);
int pos ;
while ( (pos = temp.find(des) ) < temp.size() && temp.find(des) != -1)
{
str.replace(pos,des.size(),targ) ;
temp.replace(pos,des.size(),targ) ;
}
cout << str << endl ;
}
return 0 ;
}
我其实也是参考别人写的
这里有两个函数transform 和 replace
transform是algorithm下面的函数,用来转换大小写的是个重载函数,但是我只会一种,4个参数,(容器1.begin,容器1.end,容器2.begin,大小写转换)。
replace string下面的函数,3个参数(pos,size,target) ;