题目内容:
输入2 个字符串S1 和S2,要求删除字符串S1 中出现的所有子串S2,即结果字符串中不能包含S2。
提示:输入数据的设计使得不可能出现输出为空的情况。
输入格式:
输入分别给出2 个每个不超过80 个字符长度的不带空格的非空字符串,对应S1 和S2。
输出格式:
在一行中输出删除字符串S1 中出现的所有子串S2 后的结果字符串。
输入样例:
Thisisatest is
输出样例:
Thatest
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
//char s[]="hello world";
//printf("%s\n",s);
char s1[80]={0,};
char s2[80]={0,};
int i,num;
scanf("%s",s1);
scanf("%s",s2);
//char *p=strstr(s1,s2);
while (strstr(s1,s2)){
i=0;
num=strlen(s1);
char *p=strstr(s1,s2); //返回在s1中找到的第一个s2字符串地址
int cnt=strlen(p); //cnt等于所找到的当前s2字符串地址之后所有的字符串个数
while (i!=strlen(s2)){
*p++='\0'; //对p中的s2字符串置'\0',并将p移至下一个地址
i++;
}
strcpy(s1+(num-cnt),p); //往前移位,即num-cnt为当前s1字符串的字符个数
}
num=strlen(s1);
// printf("%s\n",p);
for (i=0;i<=num;i++){
printf("%c",s1[i]);
}
return 0;
}