【c代码示例】连接两字符串

626阅读 0评论2012-06-30 niexining
分类:C/C++

以下代码取自《C陷阱与缺陷》一书 C traps and pitfalls

点击(此处)折叠或打开

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

  3. void strplus(char *s, char *t) {
  4.   char *r, *malloc();
  5.   r = malloc(strlen(s)+strlen(t)+1);
  6.   strcpy(r,s);
  7.   strcat(r,t);
  8.   printf("%d, %d, %s\n",strlen(s),strlen(t),r);
  9.   free(r);

  10. }

2.  反转字符串

点击(此处)折叠或打开

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

  3. void strv(char *s) {
  4.   int i, x=strlen(s);
  5.   char *r, *malloc();
  6.   r=malloc(x+1);
  7.   for (i=0;i<x;i++) r[i]=s[x-1-i];
  8.   r[x]='\0';
  9.   printf("the reversion of %s is %s .\n", s, r);
  10.   free(r);
  11. }


上一篇:某企业笔试题-使用加减乘除和必要的循环实现开根号运算到指定精度
下一篇:python3走迷宫