#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * join1(char *str1, char *str2);
char * join2(char *str1, char *str2);
int main()
{
char *str1 ="abc";
char *str2 = "def";
char *result = join2(str1, str2);
if(result == NULL)
{
printf("error\n");
return 0;
}
printf("%s\n", result);
free(result);
result = NULL;
return 0;
}
char * join1(char *str1, char * str2)
{
char *result = (char *)malloc(strlen(str1) + strlen(str2) + 1);
char *c = result;
if(result == NULL)
return NULL;
while(*str1 != '\0')
{
*result++ = *str1++;
}
while(*str2 != '\0')
{
*result++ = *str2++;
}
return c;
}
/*调用库函数*/
char *join2(char *str1, char *str2)
{
char *result = (char *)malloc(strlen(str1) + strlen(str2) + 1);
if(result == NULL)
return NULL;
strcpy(result, str1);
printf("%s\n", result);
strcat(result, str2);
return result;
}
C 字符串相加
最新推荐文章于 2024-04-03 11:01:25 发布