Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1075683
  • 博文数量: 155
  • 博客积分: 2525
  • 博客等级: 大尉
  • 技术积分: 2242
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-05 20:52
文章分类

全部博文(155)

文章存档

2013年(1)

2012年(149)

2011年(5)

分类:

2012-07-02 11:02:51

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


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