7-2 删除字符串中的子串 (20 分)
输入2个字符串S1和S2,要求删除字符串S1中出现的所有子串S2,即结果字符串中不能包含S2。
输入格式:
输入在2行中分别给出不超过80个字符长度的、以回车结束的2个非空字符串,对应S1和S2。
输出格式:
在一行中输出删除字符串S1中出现的所有子串S2后的结果字符串。
输入样例:
Tomcat is a male ccatat
cat
输出样例:
Tom is a male
#include <iostream>
#include <cstdio>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <stack>
#include <list>
#include <vector>
#include <set>
#include <queue>
using namespace std;
typedef long long ll;
int main(){
string s1,s2;
getline(cin,s1);
getline(cin,s2);
while(s1.find(s2) != string::npos){
s1.erase(s1.find(s2),s2.length());
}
cout<<s1<<endl;
return 0;
}
利用find函数完成对字符串的查找操作
注意:
查找字符串a是否包含子串b,不是用strA.find(strB) > 0 而是 strA.find(strB) != string:npos
其中string:npos是个特殊值,说明查找没有匹配