题目描述
给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串。
输入
输入只有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;
}
}
思路
题目的意思是比较字符串 in 时 In iN IN的情况也要进行考虑,但我实在是懒得将所有情况计算了,所以我的思路是将所有输入改为大写字母然后再进行比较。
代码应该不难看懂,这里写的时候借鉴了两个博主的文章
1.将字符串中的所有符合的子串找出并删除这些子串
https://blog.csdn.net/yishizuofei/article/details/79059804
2.将字符串全部转为大写字母
http://www.cnblogs.com/balingybj/p/4678850.html