要效率好的話,就別自己造輪子,調用現有的就好,以下程式碼供學習參考。
Code Snippet
- /************************************************************************/
- /* */
- /* file : string.c */
- /* date : 2012.3.7 */
- /* compiler : Visual C++ 2010 */
- /* author : EdisonX / Edison.Shih */
- /* */
- /************************************************************************/
- #include <stdlib.h> // size_t
- //
- // [01] strlen
- //
- size_t strlen(const char* p)
- {
- const char* beg = p;
- while(*p) ++p;
- return p-beg;
- }
- //
- // [02] strcpy
- //
- char* strcpy(char* des, const char* src)
- {
- char * ret = des;
- while(*des++ = *src++);
- return ret;
- }
- //
- // [03] strncpy
- //
- char* strncpy(char* des, const char* src, size_t num)
- {
- char * ret = des;
- while(*src && num-- && (*des++=*src++));
- *des=0;
- return ret;
- }
- //
- // [04] strcat
- //
- char * strcat ( char * des, const char * src )
- {
- char * ret = des;
- while(*des) ++des;
- while(*des++ = *src++);
- return ret;
- }
- //
- // [05] strncat
- //
- char * strncat ( char * des, const char * src , size_t num)
- {
- char * ret = des;
- while(*des) ++des;
- while(*src && num-- && (*des++ = *src++));
- return ret;
- }
- //
- // [06] strcmp
- //
- int strcmp ( const char * str1, const char * str2 )
- {
- while(*str1 && *str2 && *str1==*str2)++str1, ++str2;
- return *str1-*str2;
- }
- //
- // [07] strncmp
- //
- int strcmp ( const char * str1, const char * str2, size_t num )
- {
- while(*str1 && *str2 && num-- && *str1==*str2)++str1, ++str2;
- return *str1-*str2;
- }
- //
- // [08] strchr
- //
- char * strchr ( const char * p, int ch)
- {
- char c = (char)ch;
- while( *p && *p++!=ch) ;
- return (*p) ? p : NULL;
- }
- //
- // [09] strrchr
- //
- char * strrchr ( const char * p, int ch)
- {
- char * ret = (char*)p;
- do{
- if(*p==ch) ret = (char*)p;
- }while(*p++);
- return p;
- }
- //
- // [10] strcspn
- //
- size_t strcspn ( const char * str, const char * notin )
- {
- const char * s1 ;
- const char * s2 ;
- for(s1 = str ; *s1 ; ++s1){
- for(s2 = notin; *s2 && *s2!=*s1 ; ++s2);
- if(*s2) break;
- }
- return s2 - str;
- }
- //
- // [11] strpbrk
- //
- char * strpbrk ( const char * str, const char * brk)
- {
- const char * s1 = str;
- while( *str){
- for(s1 = brk; *s1 && *s1!=*str ; ++s1);
- if(*s1) return (char*)str;
- ++str;
- }
- return NULL;
- }
- //
- // [12] strspn
- //
- size_t strspn(const char * s1, const char * s2)
- {
- const char * p = s1 , * span;
- char c, sc;
- c=*p++;
- for(span = s2 ; sc = *span++ ; ){
- if(sc==c) {
- c = *p++;
- span = s2;
- }
- return p-s1-1;
- }
- }
- //
- // [13] strstr
- //
- char * strstr ( const char * str1, const char * str2)
- {
- char *cp = (char * )str2;
- char *s1, *s2;
- if(!*str2) return str1;
- while(*cp){
- for(s1 = cp, s2=str2 ; *s1 && *s2 && *s1!=*s2 ; ++s1, ++s2);
- if(*s2) return cp;
- ++cp;
- }
- return NULL;
- }
- //
- // [14] strtok
- //
- char * strtok(char * s, const char * delim)
- {
- char *spanp;
- int c, sc;
- char *tok;
- static char * last;
- if(!s && (s=last) == NULL) return;
- c = *s++;
- for(spanp = (char*)delim ; (sc = *spanp++) ; ){
- if(c==sc){
- c = *s++;
- spanp = (char*)delim;
- }
- }
- if(c==0) return (last = NULL);
- tok = s - 1;
- while(1){
- c = *s++;
- spanp = (char * )delim;
- do{
- if( (sc==*spanp++)==c){
- if(!c) s = NULL;
- else s[-1] = 0;
- last = NULL;
- return (tok);
- }
- }while(sc);
- }
- }
- //
- // [15] remove_char
- //
- char * remove_char(char * str , char c)
- {
- char * p , *q;
- for(p = q = str; *p ; ++p)
- if(*p != c) *q++ = *p;
- *q = 0;
- return str;
- }
全站熱搜
留言列表