【codeup】 1808 字符串的查找与删除

题目描述

给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串。

输入

输入只有1组数据。
输入一个短字符串(不含空格),再输入若干字符串直到文件结束为止。

输出

删除输入的短字符串(不区分大小写)并去掉空格,输出。

样例输入

in
#include
int main()
{

printf(" Hi ");
}

样例输出

#clude
tma()
{

prtf(“Hi”);
}

提示

注:将字符串中的In、IN、iN、in删除。

解答

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main(void){
	string str;
	string input1,input2;
	getline(cin,str);
	transform(str.begin(), str.end(), str.begin(), ::toupper);
//	cout << str << endl;
	while(getline(cin,input1)){
		int pos = 0;
		while ((pos = input1.find(' ')) != -1){
    	    input1.erase(pos,1);
    	}
    	input2 = input1;
    	transform(input2.begin(), input2.end(), input2.begin(), ::toupper);
//    	cout << input2 << endl;
    	while ((pos = input2.find(str)) != -1){
    		int length;
    		length = str.size();
    	    input1.erase(pos,length);
    		input2 = input1;
    		transform(input2.begin(), input2.end(), input2.begin(), ::toupper);
    	}
    	cout << input1 << endl;
	}
}

思路

题目的意思是比较字符串 inIn iN IN的情况也要进行考虑,但我实在是懒得将所有情况计算了,所以我的思路是将所有输入改为大写字母然后再进行比较。
代码应该不难看懂,这里写的时候借鉴了两个博主的文章
1.将字符串中的所有符合的子串找出并删除这些子串
https://blog.csdn.net/yishizuofei/article/details/79059804
2.将字符串全部转为大写字母
http://www.cnblogs.com/balingybj/p/4678850.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值