Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72173
  • 博文数量: 22
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 134
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-27 22:31
个人简介

梦想是要有的,万一实现了呢!

文章分类

全部博文(22)

文章存档

2015年(22)

分类: C/C++

2015-05-23 21:24:08


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

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

  5. /**
  6.  ** C语言实现字符串拼接
  7.  **/
  8. int main(void)
  9. {
  10.      char *ch1 = "string_";
  11.      char *ch2 = "_contact";
  12.      char *res = NULL;
  13.      res = str_contact(ch1,ch2);
  14.      printf("res = %s\n",res);
  15.      free(res);
  16.      res = NULL;
  17. }

  18. /**
  19.  ** 字符串拼接方法
  20.  **/
  21. char * str_contact(const char *str1,const char *str2)
  22. {
  23.      char * result;
  24.      result = (char*)malloc(strlen(str1) + strlen(str2) + 1); //str1的长度 + str2的长度 + \0;
  25.      if(!result){ //如果内存动态分配失败
  26.         printf("Error: malloc failed in concat! \n");
  27.         exit(EXIT_FAILURE);
  28.      }
  29.      strcpy(result,str1);
  30.      strcat(result,str2); //字符串拼接
  31.     return result;
  32. }

 

阅读(1068) | 评论(0) | 转发(0) |
0

上一篇:mysql多实例的配置和管理

下一篇:没有了

给主人留下些什么吧!~~