c语言字符串拼接变量,C语言实现字符串拼接

您所在的位置:网站首页 c语言字符串与数字拼接 c语言字符串拼接变量,C语言实现字符串拼接

c语言字符串拼接变量,C语言实现字符串拼接

2024-03-13 10:50| 来源: 网络整理| 查看: 265

#include

#include

#include

char* str_contact(const char*,const char*);

/**

** C语言实现字符串拼接

**/

int main(void)

{

char *ch1 = "hui_";

char *ch2 = "_heihei";

char *res = NULL;

res = str_contact(ch1,ch2);

printf("res = %s\n",res);

free(res);

res = NULL;

}

/**

** 字符串拼接方法

**/

char * str_contact(const char *str1,const char *str2)

{

char * result;

result = (char*)malloc(strlen(str1) + strlen(str2) + 1); //str1的长度 + str2的长度 + \0;

if(!result){ //如果内存动态分配失败

printf("Error: malloc failed in concat! \n");

exit(EXIT_FAILURE);

}

strcpy(result,str1);

strcat(result,str2); //字符串拼接

return result;

}

原文:http://www.cnblogs.com/xiaohuihui123/p/4885625.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3