【hihoCoder】#1082 : 然而沼跃鱼早就看穿了一切

时间限制: 1000ms
单点时限: 1000ms
内存限制: 256MB

描述


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

思路:存储两个字符串,一个是原始的str1,一个是将原始字符串中的大写字母都变成小写字母的字符串str2,每次匹配都是用str2去匹配,然后得到位置,然后替换的时候是str1和str2相同位置都替换掉,最后输出str1即可。

c++代码(0ms)
#include<iostream>
#include<string>
#include<cctype>
using namespace std;

int main(){
	char str[200];
	char lower_str[200];
	string s1="marshtomp";
	string s2="fjxmlhx";
	while(cin.getline(str,200)){
		string tmp(str);   //原始字符串 
		//char *s0=strlwr(str);   //将大写字符串转化为小写 
		
		string s=tmp;  //都为小写的原始字符串
		for(int i=0; i<s.size(); i++){
			s[i] = tolower(s[i]);
		}//for
		
		int len=s.length();  //len存储要求的字符串长度
		int position=s.find(s1);

		while(position !=-1){  //说明字符串中有要替换的 
			tmp = tmp.replace(position, 9, s2);
			s = s.replace(position, 9, s2);
			position=s.find(s1);
		}
		cout<<tmp<<endl; 
	}//while
	return 0;
}


一开始用strlwr 函数报错,编译不过,改成用tolower就可以了



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值