要效率好的話,就別自己造輪子,調用現有的就好,以下程式碼供學習參考。

 

Code Snippet
  1. /************************************************************************/
  2. /*                                                                      */
  3. /*     file         : string.c                                          */
  4. /*     date         : 2012.3.7                                          */
  5. /*     compiler     : Visual C++ 2010                                   */
  6. /*     author       : EdisonX / Edison.Shih                             */
  7. /*                                                                      */
  8. /************************************************************************/
  9. #include <stdlib.h> // size_t
  10.  
  11. //
  12. // [01] strlen
  13. //
  14. size_t strlen(const char* p)
  15. {
  16.     const char* beg = p;
  17.     while(*p) ++p;
  18.     return p-beg;
  19. }
  20.  
  21. //
  22. // [02] strcpy
  23. //
  24. char* strcpy(char* des, const char* src)
  25. {
  26.     char * ret = des;
  27.     while(*des++ = *src++);
  28.     return ret;
  29. }
  30.  
  31. //
  32. // [03] strncpy
  33. //
  34. char* strncpy(char* des, const char* src, size_t num)
  35. {
  36.     char * ret = des;
  37.     while(*src && num-- && (*des++=*src++));
  38.     *des=0;
  39.     return ret;
  40. }
  41.  
  42. //
  43. // [04] strcat
  44. //
  45. char * strcat ( char * des, const char * src )
  46. {
  47.     char * ret = des;
  48.     while(*des) ++des;
  49.     while(*des++ = *src++);
  50.     return ret;
  51. }
  52.  
  53. //
  54. // [05] strncat
  55. //
  56. char * strncat ( char * des, const char * src , size_t num)
  57. {
  58.     char * ret = des;
  59.     while(*des) ++des;
  60.     while(*src && num-- && (*des++ = *src++));
  61.     return ret;
  62. }
  63. //
  64. // [06] strcmp
  65. //
  66. int strcmp ( const char * str1, const char * str2 )
  67. {
  68.     while(*str1 && *str2 && *str1==*str2)++str1, ++str2;
  69.     return *str1-*str2;
  70. }
  71.  
  72. //
  73. // [07] strncmp
  74. //
  75. int strcmp ( const char * str1, const char * str2, size_t num )
  76. {
  77.     while(*str1 && *str2 && num-- && *str1==*str2)++str1, ++str2;
  78.     return *str1-*str2;
  79. }
  80.  
  81. //
  82. // [08] strchr
  83. //
  84. char * strchr ( const char * p, int ch)
  85. {
  86.     char c = (char)ch;
  87.     while( *p && *p++!=ch) ;
  88.     return (*p) ? p : NULL;
  89. }
  90.  
  91. //
  92. // [09] strrchr
  93. //
  94. char * strrchr ( const char * p, int ch)
  95. {
  96.     char * ret = (char*)p;
  97.     do{
  98.         if(*p==ch) ret = (char*)p;
  99.     }while(*p++);
  100.     return p;
  101. }
  102.  
  103. //
  104. // [10] strcspn
  105. //
  106. size_t strcspn ( const char * str, const char * notin )
  107. {
  108.     const char * s1 ;
  109.     const char * s2 ;
  110.     for(s1 = str ; *s1 ; ++s1){
  111.         for(s2 = notin; *s2 && *s2!=*s1 ; ++s2);
  112.         if(*s2) break;
  113.     }
  114.     return s2 - str;    
  115. }
  116.  
  117. //
  118. // [11] strpbrk
  119. //
  120. char * strpbrk ( const char * str, const char * brk)
  121. {
  122.     const char * s1 = str;
  123.     while( *str){
  124.         for(s1 = brk; *s1 && *s1!=*str ; ++s1);
  125.         if(*s1) return (char*)str;
  126.         ++str;
  127.     }
  128.     return NULL;
  129. }
  130.  
  131. //
  132. // [12] strspn
  133. //
  134. size_t strspn(const char * s1, const char * s2)
  135. {
  136.     const char * p = s1 , * span;
  137.     char c, sc;
  138.  
  139.     c=*p++;
  140.     for(span = s2 ; sc = *span++ ; ){
  141.         if(sc==c) {
  142.             c = *p++;
  143.             span = s2;
  144.         }
  145.         return p-s1-1;
  146.     }
  147. }
  148.  
  149. //
  150. // [13] strstr
  151. //
  152. char * strstr ( const char * str1, const char * str2)
  153. {
  154.     char *cp = (char * )str2;
  155.     char *s1, *s2;
  156.  
  157.     if(!*str2) return str1;
  158.     while(*cp){
  159.         for(s1 = cp, s2=str2 ; *s1 && *s2 && *s1!=*s2 ; ++s1, ++s2);
  160.         if(*s2) return cp;
  161.         ++cp;
  162.     }
  163.     return NULL;
  164.  
  165. }
  166.  
  167. //
  168. // [14] strtok
  169. //
  170. char *    strtok(char * s, const char * delim)
  171. {
  172.     char *spanp;
  173.     int c, sc;
  174.     char *tok;
  175.     static char * last;
  176.  
  177.     if(!s && (s=last) == NULL) return;
  178.  
  179.     c = *s++;
  180.     for(spanp = (char*)delim ; (sc = *spanp++) ; ){
  181.         if(c==sc){
  182.             c = *s++;
  183.             spanp = (char*)delim;
  184.         }
  185.     }
  186.  
  187.     if(c==0) return (last = NULL);
  188.     tok = s - 1;
  189.     while(1){
  190.         c = *s++;
  191.         spanp = (char * )delim;
  192.         do{
  193.             if( (sc==*spanp++)==c){
  194.                 if(!c) s = NULL;
  195.                 else s[-1] = 0;
  196.                 last = NULL;
  197.                 return (tok);
  198.             }
  199.         }while(sc);        
  200.     }
  201. }
  202.  
  203. //
  204. // [15] remove_char
  205. //
  206.  
  207. char * remove_char(char * str , char c)
  208. {
  209.     char * p , *q;
  210.     for(p = q = str; *p ; ++p)
  211.         if(*p != c) *q++ = *p;
  212.     *q = 0;
  213.     return str;
  214. }
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 edisonx 的頭像
    edisonx

    Edison.X. Blog

    edisonx 發表在 痞客邦 留言(2) 人氣()