急求用c语言编写两字符串的并集

例如 a = “abcde”,b = “cdefgh” 那么结果为 c = “abcdefgh”

第1个回答  2010-09-17
举个例子给你,其实可以用C的标准库函数,不过自己写最好了,比较简单,练下手.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str1="the first string";
char *str2="the second string";
char *strs;
int i=0;
strs=(char *)malloc(sizeof(char));
strs[0]='\0';
while(*str1!='\0')
{
strs[i]=*str1;
i++;
strs=(char *)realloc(strs,(i+1)*sizeof(char));
strs[i]='\0';
str1++;
}

//将字符串2复制进str
while(*str2!='\0')
{
strs[i]=*str2;
i++;
strs=(char *)realloc(strs,(i+1)*sizeof(char));
strs[i]='\0';
str2++;
}
printf("%s\n",strs);
}
相似回答