7-10 删除字符串中的子串(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<stdio.h>
#include<string.h>
int main()
{
char a[100],b[100],c[81];
char *w;
int i,j;
gets(a);
gets(b);
for(i=0;;i++)
{
if((strstr(a,b))==NULL)//从字符串str1中查找是否有字符串str2, 如果有,从str1中的str2位置起,返回str1的指针,如果没有,返回null。
break;
else
{
w = strstr(a,b);
*w = '\0';//划短字符串,并加0
strcpy(c,w+strlen(b));//从w+strlen()开始从地址c拷贝字符串
strcat(a,c);
}
}
puts(a);
return 0;
}
/*
Tom is a male ccatat
Tom is a male cat
Tom is a male
*/