zoukankan      html  css  js  c++  java
  • strcmp实现

    #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 == '') 
                       return 0;
            }
          return ((*s1<*s2)?-1:1);
    }
    
    
    int main()
    {
        char *s1="abc",*s2="abcd";
        int m;
        m=my_strcmp4(s1,s2);
        printf("m=%d
    ",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=='')
    returnc1-c2;
    }while(c1==c2);
    returnc1-c2;
    }
    libc_hidden_builtin_def(strcmp)
     
     
     
  • 相关阅读:
    Vue.2.0.5-混合
    Vue.2.0.5-自定义指令
    Vue.2.0.5-过渡状态
    Vue.2.0.5-深入响应式原理
    spring mvc + mybatis + spring aop声明式事务管理没有作用
    Spring注解@Component、@Repository、@Service、@Controller区别
    继承之后的使用注意事项_ArrayStoreException
    java 中打印调用栈
    禁止 favicon.ico 请求
    mysq时间戳最小值
  • 原文地址:https://www.cnblogs.com/timssd/p/4091015.html
Copyright © 2011-2022 走看看