C/C++函式,比較兩個字串
設這兩個字串為str1,str2,
若str1==str2,則返回零;
若str1<str2,則返回負數;
若str1>str2,則返回正數。
extern int strcmp(const char *s1,const char *s2);
int strcmp(const char *str1,const char *str2)
{
/*不可用while(*str1++==*str2++)來比較,當不相等時仍會執行一次++,
return返回的比較值實際上是下一個字元。應將++放到迴圈體中進行。*/
while(*str1 == *str2)
if(*str1 == "\0")
return0;
str1++;
str2++;
}
return *str1 - *str2;
int strcmp(p1,p2)
const char *p1;
const char *p2;
register const unsignedchar *s1=(const unsignedchar*)p1;
register const unsignedchar *s2=(const unsignedchar*)p2;
unsigned reg_charc1,c2;
do{
c1=(unsigned char)*s1++;
c2=(unsigned char)*s2++;
if(c1=="\0")
returnc1-c2;
}while(c1==c2);
return c1-c2;
libc_hidden_builtin_def(strcmp)
//最正宗的原始碼(他以上程式碼是K&R C規範的,ASCI C的在下面)
/*strcmp function*/
#include <string.h>
int(strap)(const char *sl,const char *s2)//****************************mark
/*compare unsigned char sl[],s2[]*/
for(;*sl==*s2;++sl,++s2)
if(*sl=="\0")
return(0);
return((*(unsignedchar*)sl<*(unsignedchar*)s2)?-1:+1);
strcmp另外的原始碼如下:
int __cdecl strcmp(const char *src,const char *dst)
int ret=0;
while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src,++dst;
if(ret<0)
ret=-1;
else if(ret>0)
ret=1;
return(ret);
C/C++函式,比較兩個字串
設這兩個字串為str1,str2,
若str1==str2,則返回零;
若str1<str2,則返回負數;
若str1>str2,則返回正數。
extern int strcmp(const char *s1,const char *s2);
int strcmp(const char *str1,const char *str2)
{
/*不可用while(*str1++==*str2++)來比較,當不相等時仍會執行一次++,
return返回的比較值實際上是下一個字元。應將++放到迴圈體中進行。*/
while(*str1 == *str2)
{
if(*str1 == "\0")
return0;
str1++;
str2++;
}
return *str1 - *str2;
}
int strcmp(p1,p2)
{
const char *p1;
const char *p2;
register const unsignedchar *s1=(const unsignedchar*)p1;
register const unsignedchar *s2=(const unsignedchar*)p2;
unsigned reg_charc1,c2;
do{
c1=(unsigned char)*s1++;
c2=(unsigned char)*s2++;
if(c1=="\0")
returnc1-c2;
}while(c1==c2);
return c1-c2;
}
libc_hidden_builtin_def(strcmp)
//最正宗的原始碼(他以上程式碼是K&R C規範的,ASCI C的在下面)
/*strcmp function*/
#include <string.h>
int(strap)(const char *sl,const char *s2)//****************************mark
{
/*compare unsigned char sl[],s2[]*/
for(;*sl==*s2;++sl,++s2)
if(*sl=="\0")
return(0);
return((*(unsignedchar*)sl<*(unsignedchar*)s2)?-1:+1);
}
strcmp另外的原始碼如下:
int __cdecl strcmp(const char *src,const char *dst)
{
int ret=0;
while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src,++dst;
if(ret<0)
ret=-1;
else if(ret>0)
ret=1;
return(ret);
}