云袭2001's博客地址迁移啦——attacker.cc
输入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(void)
{
char s[100] = {0};
char del[100] = {0};
char temp[100] = {0};
gets(s);
gets(del);
while( strstr(s, del) != NULL)
{
int len_s = strlen(s);
int len_del = strlen(del);
strcpy(temp, strstr(s, del));
int loc = strlen(temp);
// 关键的地方就是这一句
strcpy(s + len_s - loc, temp + len_del);
}
puts(s);
return 0;
}
方法(2)
//#include <stdio.h>
//#include <string.h>
//
//int main(void)
//{
// char s1[1000], s2[1000], temp[1000] = {0};
// char *p;
// gets(s1); gets(s2);
//
// while( (p = strstr(s1, s2)) != NULL)
// {
// *p = '\0'; //截掉s1指针后面的内容
// strcat(temp, s1); // 把s1中与s2相同的前一部分放在temp中
// strcat(temp, p + strlen(s2)); //把指针后移s2的长度,在temp后面粘贴s1剩余的数组
// // 上面两步,剔除s1中与s2重合的部分,同时存在temp中
// strcpy(s1, temp);
// temp[0] = '\0'; //清空temp数组
// }
//
// printf("%s\n", s1);
// return 0;
//}