输入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 <cstring>
#include <cstdio>
#include <string>
//找出子串 并删除
using namespace std;
char* f1(char a[],char b[]);
void del(char a[],char b[]);
int main()
{
char a[200];
char b[80];
gets(a);
gets(b);
del(a,b);
cout<<a;
return 0;
}
char* f1(char a[],char b[])
{
int i=0,j=0,k=0;
for (i=0;a[i]!='\0';i++)
{
if (a[i]==b[0])
{
k=i+1;
for (j=1;a[k]==b[j]&&a[k]!='\0';k++,j++);//寻找子串
if (b[j]=='\0')
return a+i;
}
}
return 0;
}
void del(char a[],char b[])
{
char* p;
while (p=f1(a,b))
strcpy(p,p+strlen(b));
}