yyyyyyyyyyyyyyyyyyyy

博客园 首页 新随笔 联系 订阅 管理
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char*a,const char*b)
{
    while(*a&&*b&&*a==*b)
        a++,b++;
    if(*a&&*b)
        return(*a-*b);
    else if(*a&&*b==0)
        return 1;
    else if(*a==0&&*b)
        return-1;
    return 0;
}

int my_strcmp1(const char *str1, const char *str2)  
{  
     assert((str1 != NULL) && (str2 != NULL));  
     int rt = 0;  
     while ( !(rt = *(unsigned char *)str1 - *(unsigned char*)str2 ) &&  *str2)  
     {  
         str1 ++;   
         str2 ++;  
     }  
     if (rt > 0)   
     {  
         rt = 1;  
     }  
     else if (rt < 0)  
     {  
         rt = -1;  
     }  
     return rt;  
}  


int my_strcmp2(unsigned char *src, unsigned char *dst)
{
        int ret = 0 ;
        while( ! (ret = *src - *dst) && *dst)
             ++src, ++dst;
        if ( ret < 0 )
              ret = -1 ;
        else if ( ret > 0 )
              ret = 1 ;
        return( ret );
}

int my_strcmp3(const char *dest, const char *source)  
{  
   assert((NULL != dest) && (NULL != source));  
   while (*dest && *source && (*dest == *source))  
           {  
                    dest ++;  
                   source ++;  
           }  
   return *dest - *source;
}  


int my_strcmp4(const char* s1,const char * s2)
{
      for(;*s1==*s2;s1++,s2++)      
        {
            if(*s1 == '\0') 
                   return 0;
        }
      return ((*s1<*s2)?-1:1);
}


int main()
{
    char *s1="abc",*s2="abcd";
    int m;
    m=my_strcmp4(s1,s2);
    printf("m=%d\n",m);
}

其它:
intstrcmp(p1,p2)
{
constchar*p1;
const char *p2;
{
registerconstunsignedchar*s1=(constunsignedchar*)p1;
registerconstunsignedchar*s2=(constunsignedchar*)p2;
unsignedreg_charc1,c2;
do{
c1=(unsignedchar)*s1++;
c2=(unsignedchar)*s2++;
if(c1=='\0')
returnc1-c2;
}while(c1==c2);
returnc1-c2;
}
libc_hidden_builtin_def(strcmp)
 
 
 

 

posted on 2014-11-12 02:23  xxxxxxxx1x2xxxxxxx  阅读(227)  评论(0编辑  收藏  举报