合并两个字符集合为一个新集合,每个字符串在新集合中仅出现一次,函数返回新集合中字符串。
如:
s1集合{“while”,”for”,”switch”,”if”,”continue”}
s2集合{“for”,”case”,”do”,”else”,”char”,”switch”}
运行结果:
while for switch if break continue case do else char
#include <stdio.h>
#include <math.h>
void string_cmp(char (*str)[10],char (*str1)[10],char (*str2)[10],int n,int m)
{
int i;
int j;
int len;
char (*p)[m];//瀹氫箟涓€涓寚閽堟暟缁?
p = str2;
for(i = 0,len = 0; i < n; i++,len++)//灏嗘暟缁剆tr1鎷峰埌str涓? {
strcpy(str[i],str1[i]);
}
for(i = 0; i < m; i++)//鍒ゆ柇str1鍜宻tr2涓槸鍚︽湁鐩稿悓鍏冪礌
{
for(j = 0; j < n; j++)
{
if(strcmp(str2[i],str1[j]) == 0)
{
strcpy(str2[i],"ok");//鐩稿悓鐨勫厓绱犵敤ok浠f浛
}
}
}
for(i = 0; i < m; i++)//灏唖tr2涓簬str1涓笉鐩稿悓鐨勫厓绱犲鍒跺埌str涓? {
if(strcmp(str2[i],"ok") != 0)
{
strcpy(str[len],str2[i]);
len++;
}
}
for(i = 0; i < len; i++ )//鎵撳嵃str
{
printf("%s\n",str[i]);
}
}
int main()
{
char str[10][10];
char str1[][10] = {"while","for","switch","if","continue"};
char str2[][10] = {"for","case","do","else","char","switch"};
int n = 5;
int m = 6;
string_cmp(str,str1,str2,n,m);//璋冪敤鍑芥暟string_cmp
return 0;
}