Comparison of two strings
My block of code
#include <string.h>intMY_STRCMP (const char* s1,const char * s2) {if((s1==null) | | (S2==null))return 0; while(1) {if((*s1==' + ')|| (*s2==' + ')) Break;if(*s1>*s2)return 1;if(*s1<*s2)return-1; s1++; s2++; } //if(*s1==*s2==' + ') //return 0;if(*s1==' + '&&*s2==' + ')return 0;if(*s1>*s2)return 1;Else return-1;}intMain () {char* a="ABCdef"; char* b="ABCdef"; MY_STRCMP (A, b);return 0;}
Here, the above method, unreliable, without definite intrinsic logic,
Look at the source
/* Copyright (C)1991- -Free Software Foundation, Inc. This file isPart ofThe GNU C Library. The GNU C Library isFree software; You can redistribute it and/orModify it under the terms ofThe GNU Lesser general public License as published byThe free software Foundation; Either version2.1 ofThe License,or(at your option) any later version. The GNU C Library isDistributedinchThe hope that it'll be useful, but without any WARRANTY; Without even the implied warranty ofmerchantabilityorFITNESS for A particular PURPOSE. See the GNU Lesser general public License forMore details. You should has received a copy ofThe GNU Lesser general public License along withThe GNU C Library;if not, see <http://www.gnu.org/licenses/. */#include <string.h>#undef strcmp/* Compare S1 andS2, returning less than, equal toorGreater than zeroifS1 islexicographically less than, equal toorGreater than S2. */INTSTRCMP (ConstChar *p1,ConstChar *p2) {Constunsigned char *s1 = (Constunsigned char *) p1;Constunsigned char *s2 = (Constunsigned char *) P2; unsigned char C1, C2; Do{C1 = (unsigned char) *s1++; C2 = (unsigned char) *s2++;if(C1 = =' + ')returnC1-C2; } while(C1 = = C2);returnC1-C2;}
To compare the size of each byte of the S1 and S2, the simplest classification is equal and unequal, and returns are calculated immediately when they are equal and not equal.
As for
Const unsigned char s1 = (const unsigned char ) p1;
const unsigned char s2 = (const unsigned char ) P2;
The beauty of such conversion, failed to comprehend
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
GNU libc Source Learning strcmp