Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1754235
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: C/C++

2011-10-08 18:13:51

  1. #include <ctype.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <assert.h>
  5. #include <string.h>
  6. #define OK 0
  7. int mystrlen(char *);
  8. void mystrcpy(char *,char *);
  9. int bitcount(unsigned );
  10. int main(int argc,char **){
  11. char *s="this is for test of strlen and strcpy ";
  12. int len=mystrlen(s);
  13. char * d=(char *)malloc(sizeof(char)*(len+1));//notice here the space you need to allocate is the // length of string and the lastcharacter '\0'
  14. memset(d,0,len*sizeof(char));
  15. mystrcpy(d,s);
  16. printf("the length of %s is%d\r\n",s,len-1);
  17. printf("the source string is \r\n%s\r\n",s);
  18. printf("the destination string is \r\n%s\r\n",d);
  19. getchar();
  20. return OK;
  21. }
  22. int mystrlen(char * s){
  23. assert(s);
  24. char *p = s;
  25. while( *p != '\0'){
  26. p++;
  27. }
  28. return p-s;
  29. }
  30. void mystrcpy(char *d,char *s){ //cryptic of pointer
  31. assert(s);
  32. while(*d++ = *s++)
  33. ;
  34. }


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