C - Squeeze(s1,s2): Delete each character in s1 that matches any character in string s2

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net

/*
 * Write a function Squeeze(s1,s2) that deletes each character in s1
 * that matches any character in string s2.
 *
 * Squeeze.c - by FreeMan
 */

#include <stdio.h>

void Squeeze(char s1[], const char s2[]);

int main()
{
	char s1[] = "This is string s1.";
	char s2[] = "Test";
	Squeeze(s1, s2);
	printf("%s\n", s1);

	return 0;
}

void Squeeze(char s1[], const char s2[])
{
	int i, j, k;
	for (i = 0; s2[i] != '\0'; i++)
	{
		for (j = k = 0; s1[j] != '\0'; j++)
		{
			if (s1[j] != s2[i])
			{
				s1[k++] = s1[j];
			}
		}
		s1[k] = '\0';
	}
}

// Output:
/*
hi i ring 1.

*/

猜你喜欢

转载自blog.csdn.net/chimomo/article/details/113571686