李春葆 新编C语言习题与解析 4-2-25

例4-2-25

//str1和str2中各自没有重复的字符,求其交集并将其存入str3中
#define N 100
#include
#include

int main()
{
	char str1[N], str2[N], str3[N];
	int count1, count2, count3 = 0;
	printf("str1=\n");
	gets_s(str1);
	printf("str2=\n");
	gets_s(str2);

	for (count1 = 0; str1[count1] != '\0'; count1++)
	{
		for (count2 = 0; str2[count2] != '\0'; count2++)
		{
			if (str2[count2] == str1[count1])
				str3[count3++] = str1[count1];
		}
	}
	str3[count3] = '\0';

	//输出
	printf("%s\n", str3);

	return 0;

}

你可能感兴趣的:(C语言练习,c语言,开发语言,c语言)