代码实现
int mystrcmp(char * str1, char * str2){
char * p = str1;
char * q = str2;
int len1 = strlen(str1);
int len2 = strlen(str2);
if(len1 > len2){
return -1;
} else if (len1 < len2){
return -1;
}
while(('\0' != p) && ('\0' != *q)){
if(*p != *q){
if(*p > *q){
return 1;
} else if (*p < *q){
return -1;
} else {
}
}
p++;
q++;
}
return 0;
}
void testmystrcmp(void){
char * str = "1234567";
char * str1 = "1234567";
char * str2 = "12345678";
char * str3 = "1234566";
int ret = 0;
printf("\n************ testmystrcmp ************ \n");
ret = mystrcmp(str, str1);
if(0 == ret){
printf("%s and %s is the same\n", str,str1);
} else if (1 == ret) {
printf("%s big than %s \n", str,str1);
}else if (-1 == ret) {
printf("%s less than %s \n", str,str1);
} else {
}
ret = mystrcmp(str, str2);
if(0 == ret){
printf("%s and %s is the same\n", str,str2);
} else if (1 == ret) {
printf("%s big than %s \n", str,str2);
}else if (-1 == ret) {
printf("%s less than %s \n", str,str2);
} else {
}
ret = mystrcmp(str, str3);
if(0 == ret){
printf("%s and %s is the same\n", str,str3);
} else if (1 == ret) {
printf("%s big than %s \n", str,str3);
}else if (-1 == ret) {
printf("%s less than %s \n", str,str3);
} else {
}
return;
}
代码编译
gcc main.c str.c -g -o a.exe
调试输出
************ testmystrcmp ************
1234567 and 1234567 is the same
1234567 less than 12345678
1234567 big than 1234566