字符串函数

几个常见字符串处理函数

  1. /* *
  2. * str开头的函数部分
  3. * In the following functions, variables s and t are of type char *;
  4. * cs and ct are of type const char *; n is of type size_t;
  5. * and c is an int converted to char.
  6. */
  7.  
  8. // strcpy - copy string ct to string s, including '\0'; return s.
  9. char *strcpy(char *s, const char *ct)
  10. {
  11.     char *tmp = s;
  12.  
  13.     while ((*tmp++ = *ct++) != '\0')
  14.         /* nothing */;
  15.     return s;
  16. }
  17. <span id="more-18"></span>
  18. // strcat - concatenate string ct to end of string s; return s.
  19. char *strcat(char * s, const char * ct)
  20. {
  21.     char *tmp = s;
  22.  
  23.     while (*tmp)
  24.         tmp++;
  25.     while ((*tmp++ = *ct++) != '\0')
  26.         /* nothing */;
  27.  
  28.     return s;
  29. }
  30.  
  31. // strcmp - compare string cs to string ct, return <0 if cs<ct,
  32. // 0 if cs==ct, or >0 if cs>ct.
  33. int strcmp(const char *cs,const char *ct)
  34. {
  35.     register signed char __res;
  36.    
  37.     while (1) {
  38.         if ((__res = *cs - *ct++) != 0 || !*cs++)
  39.             break;
  40.     }
  41.    
  42.     return __res;
  43. }
  44.  
  45. // strlen - return length of cs.
  46. size_t strlen(const char *cs)
  47. {
  48.     const char *tmp;
  49.  
  50.     for (tmp = cs; *tmp != '\0'; ++tmp)
  51.         /* nothing */;
  52.     return tmp - cs;
  53. }
  54.  
  55.  
  56. /**
  57. * mem开头的函数部分
  58. * The mem... functions are meant for manipulating objects as character
  59. * arrays; the intent is an interface to efficient routines.
  60. * In the following functions, s and t are of type void *;
  61. * cs and ct are of type const void *; n is of type size_t;
  62. * and c is an int converted to an unsigned char.
  63. */
  64.  
  65. // memcpy - copy n characters from ct to s, and return s.
  66. void *memcpy(void *s, const void *ct, size_t n)
  67. {
  68.     char *dst = (char *)s, *src = (char *)ct;
  69.  
  70.     while (n--)
  71.         *dst++ = *src++;
  72.  
  73.     return s;
  74. }
  75.  
  76. // memmove - same as memcpy except that it works even
  77. // if the objects overlap.
  78. void *memmove(void *s, const void *ct, size_t n)
  79. {
  80.     char *dst, *src;
  81.    
  82.     if (s <= ct) {
  83.         dst = (char *)s;
  84.         src = (char *)ct;
  85.         while (n--)
  86.             *dst++ = *src++;
  87.     }
  88.     else {
  89.         dst = (char *)s + n;
  90.         src = (char *)ct + n;
  91.         while (n--)
  92.             *--dst = *--src;
  93.     }
  94.    
  95.     return s;
  96. }
  97.  
  98. // memcmp - compare the first n characters of cs with ct;
  99. // return as with strcmp.
  100. int memcmp(const void *cs, const void *ct, size_t n)
  101. {
  102.     const unsigned char *su1, *su2;
  103.     signed char res = 0;
  104.    
  105.     for( su1 = cs, su2 = ct; 0 < n; ++su1, ++su2, n--)
  106.         if ((res = *su1 - *su2) != 0)
  107.             break;
  108.     return res;
  109. }
  110.  
  111. // memset - place character c into first n characters of s, return s.
  112. void *memset(void *s, char c, size_t n)
  113. {
  114.     char *xs = (char *)s;
  115.  
  116.     while (n--)
  117.         *xs++ = c;
  118.  
  119.     return s;
  120. }

文章若无注明则属原创,转载请以链接形式注明出处。
本文地址:http://www.juliuschen.com/archives/18.html

Leave a Reply