Chinaunix首页 | 论坛 | 博客
  • 博客访问: 123196
  • 博文数量: 19
  • 博客积分: 329
  • 博客等级: 一等列兵
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-10 09:58
文章分类

全部博文(19)

文章存档

2012年(3)

2011年(14)

2009年(2)

分类: C/C++

2011-09-01 15:43:19

一直觉得自己眼高手低, 平时也懒得写代码, 下定决心要克服这个毛病,于是开始了重新拿起 the c programming languages 这本C语言圣经做做上面的练习题.就当是练手吧.


这次从字符串操作部分入手,实现自己的库,争取多写,多思考.而后看看一下coreutilitiesGNUNetBSD实现,接着看看(e)glibc,再以后,再打算吧.希望能有高手指出自己的不足,提高自己,一切以学习为目的.


  1. /*
  2.  * =====================================================================
  3.  *
  4.  * Filename: wolf_string.c
  5.  *
  6.  * Description: Defination for my str lib
  7.  *
  8.  * Version: 1.0
  9.  * Created: 2011年09月01日 13时14分35秒
  10.  * Revision: none
  11.  * Compiler: gcc
  12.  *
  13.  * Author: wolfpython(walter) (), wolfpythonlondon@gmail.com
  14.  * Company:
  15.  *
  16.  * ====================================================================
  17.  */
  18. #include "wolf_string.h"
  19. #include <assert.h>


  20. int
  21. wolf_strlen(char *src)
  22. {
  23.     int i = 0;

  24.     while(*src != '\0') {
  25.         src++;
  26.         i++;
  27.     }
  28.     
  29.     return i;
  30. }

  31. int
  32. wolf_strlen_1(char *src)
  33. {
  34.     char *p = src;

  35.     while(*src != '\0')
  36.         src++;

  37.     return src-p;
  38. }

  39. void
  40. wolf_strcpy(char *des, char *src)
  41. {
  42.     /* if des is a null pointer, its not safe to modify the mem
  43.      * if the src is a null pointer, it doesnt make any sense
  44.      * */
  45.     assert(des != NULL && src != NULL);

  46.     while((*des = *src) != '\0') {
  47.         des++;
  48.         src++;
  49.     }
  50. }


  51. void
  52. wolf_strcpy_1(char *des, char *src)
  53. {
  54.         /* if des is a null pointer, its not safe to modify the mem
  55.      * if the src is a null pointer, it doesnt make any sense
  56.      * */
  57.     assert(des != NULL && src != NULL);

  58.     while(*des++ = *src++)
  59.         ;
  60. }


  61. int
  62. wolf_strcmp(char *des, char *src)
  63. {
  64.     /* if des is a null pointer, its not safe to modify the mem
  65.      * if the src is a null pointer, it doesnt make any sense
  66.      * */
  67.     assert(des != NULL && src != NULL);

  68.     while(*des == *src) {
  69.         if(*des == '\0') {
  70.             return 0;
  71.         }
  72.         
  73.         des++;
  74.         src++;
  75.     }

  76.     return *des - *src;
  77. }


  78. void
  79. wolf_strcat(char *des, char *src)
  80. {
  81.     /* it doesnt matter if the src is a null pointer */
  82.     assert(des != NULL);

  83.     while(*des != '\0')
  84.         des++;
  85.         
  86.     while(*des++ = *src++)
  87.         ;
  88. }

  89. /*
  90.  *if t is at the end of s, return 1,
  91.  *else return 0
  92.  * */
  93. int
  94. wolf_strend(char *s, char *t)
  95. {
  96.     int tsize;
  97.     char *thead = t; // the head of t
  98.     
  99.     /* it doesnt make any sense if t is a null pointer */
  100.     assert(t != NULL);

  101.     if( s == NULL) return 0;
  102.     while(*s != '\0')
  103.         s++;

  104.     while(*t != '\0')
  105.         t++;
  106.     tsize = t - thead;
  107.     
  108.     while(tsize-- > 0) {
  109.         if(*s != *t)
  110.             return 0;
  111.         s--;
  112.         t--;
  113.     }

  114.     return 1;
  115. }
阅读(1517) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~