字符串的查找删除

 

题目链接:http://www.codeup.cn/problem.php?cid=100000580&pid=2

题目描述

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

输入

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

输出

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

样例输入

in
#include 
int main()
{

printf(" Hi ");
}

样例输出

#clude
tma()
{

prtf("Hi");
}

提示

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

关于find()、erase()、string::npos  见 https://blog.csdn.net/m0_37345402/article/details/83718774

#include<iostream>
#include<string>
using namespace std;
int main(){
	string a;
	char str[1111]; 
	cin>>a;
	getchar();
	for(int i=0;i<a.size();i++){
		a[i]=tolower(a[i]);//字符串转换为小写 
	}
	while(gets(str)){
		string b=str;
		string temp=b;//temp保存原字符串 
		for(int i=0;i<b.size();i++){
			b[i]=tolower(b[i]); 
		}
		//删除字符串a 
		int pos=b.find(a,0); //在b串中查找a的开始下标,返回索引
		while(pos!=string::npos){//find的返回如果没有找到,返回npos 
			temp.erase(pos,a.size());
			b.erase(pos,a.size());
			pos=b.find(a,pos);//从pos为起点继续查找b中下一个出现字符串a的位置 
		} 
		//删除空格 
		pos=temp.find(' ',0);
		while(pos!=string::npos){
			temp.erase(pos,1);
			pos=temp.find(' ',0);
		}
		cout<<temp<<endl;
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值