AtomSoft Well-Known Member Mar 23, 2012 #1 Would it be wise to create my own string compare function? For instance: Code: char strCompare(char *str1,char *str2, unsigned char len) { unsigned char x = 0; for(x=0;x<len;x++) { if(*str1++ != *str2++) return -1; } return 0; } You think this would give me any problems? It seems to work nice and fast. sometimes i just dont like including files or just dont understand why some things are overcomplicated.
Would it be wise to create my own string compare function? For instance: Code: char strCompare(char *str1,char *str2, unsigned char len) { unsigned char x = 0; for(x=0;x<len;x++) { if(*str1++ != *str2++) return -1; } return 0; } You think this would give me any problems? It seems to work nice and fast. sometimes i just dont like including files or just dont understand why some things are overcomplicated.
P Pommie Well-Known Member Most Helpful Member Mar 23, 2012 #2 That will work fine unless one of the strings is longer than len. You could do something like, Code: char strCompare(char *str1,char *str2){ unsigned char x = 0; do{ if(*str1 != *str2) return -1; }while(*str1++ && *str2++) return 0; } As this ensures the zero terminators are compared as well. Mike.
That will work fine unless one of the strings is longer than len. You could do something like, Code: char strCompare(char *str1,char *str2){ unsigned char x = 0; do{ if(*str1 != *str2) return -1; }while(*str1++ && *str2++) return 0; } As this ensures the zero terminators are compared as well. Mike.
K KeepItSimpleStupid Well-Known Member Most Helpful Member Apr 1, 2012 #4 Why return -1 if they are not equal? Just asking.
P Pommie Well-Known Member Most Helpful Member Apr 1, 2012 #5 It would make more sense to return 0 for a mismatch. I just copied Jason's example without really thinking about it. Mike.
It would make more sense to return 0 for a mismatch. I just copied Jason's example without really thinking about it. Mike.