输入2 个字符串S1 和S2,要求删除字符串S1 中出现的所有子串S2
题目内容:
输入2 个字符串S1 和S2,要求删除字符串S1 中出现的所有子串S2,即结果字符串中不能包含S2。
提示:输入数据的设计使得不可能出现输出为空的情况。
输入格式:
输入分别给出2 个每个不超过80 个字符长度的不带空格的非空字符串,对应S1 和S2。
输出格式:
在一行中输出删除字符串S1 中出现的所有子串S2 后的结果字符串。
输入样例:
Thisisatest is
输出样例:
Thatest
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * returnStr(char * s1, const char * s2);
char * returnStr(char * s1, const char * s2)
{
char * str = 0 ;
char * p = 0;
char * p_temp = 0;
while ((p = strstr(s1, s2)) != NULL) {
p_temp = p;
*p = '\0';
str = (char *)malloc(sizeof(s1)+1);
strcpy(str, s1);
strcat(str, p + strlen(s2));
s1 = str;
}
*p_temp = s2[0];
free(str);
str = NULL