题目链接: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;
}