[ZCMU OJ]1614: Problem 3(字符串的替换+注意大小写)

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;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值