首页 > 代码库 > strcmp实现
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 == ‘\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
)
strcmp实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。