7-16 删除字符串中的子串 (20分)

7-16 删除字符串中的子串 (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>

char s3[100];

void String_ing(char *s1, char *s2)
{
	int j = 0;
	int l = 0;
	while (strstr(s1 + l, s2))
	{
		int i = l;

		l = strstr(s1 + l, s2) - s1;

		for (; i < l; ++i)
		{
			s3[j ++] = s1[i];
		}
		l += strlen(s2);
	}
	while (s1[l])
		s3[j ++] = s1[l++];
}

int main()
{
	char s1[100];
	char s2[100];

	gets(s1);
	gets(s2);
	memset(s3, 0, sizeof(s3));

	String_ing(s1,s2);
	while (strstr(s3, s2))
	{
		strcpy(s1, s3);
		memset(s3, 0, sizeof(s3));
		String_ing(s1, s2);
	}
	puts(s3);

	return 0;
}
发布了131 篇原创文章 · 获赞 7 · 访问量 5777

猜你喜欢

转载自blog.csdn.net/wct3344142/article/details/103832708