Chinaunix首页 | 论坛 | 博客
  • 博客访问: 660635
  • 博文数量: 516
  • 博客积分: 4119
  • 博客等级: 上校
  • 技术积分: 4288
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-30 17:29
文章分类

全部博文(516)

文章存档

2014年(4)

2013年(160)

2012年(352)

分类:

2012-11-01 11:22:26

以下代码取自《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. }


阅读(182) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~