字符串-02. 删除字符串中的子串(20)

云袭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;
//}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值