判断两个字符串的大小
1 #include <stdio.h> 2 3 int my_strcmp(const char *str1,const char *str2) 4 { 5 //判断两个字符串是否为空 6 if((str1 == NULL) && (str2 == NULL)) 7 { 8 exit(0); 9 } 10 //利用while循环,str1不为零,判定str1的值是否等于str2的值 11 while((*str1) && ((*str1==*str2))) 12 { 13 str1++; 14 str2++; 15 } 16 17 //如果str1大于str2,第一字符串大于第二个字符串 18 if(*(unsigned char *)str1 > *(unsigned char *)str2) 19 { 20 printf("The first string is greater than the second string\n"); 21 return 1; 22 } 23 //如果str1小于str2,第一个字符串小于第二个字符串 24 else if(*(unsigned char *)str1 < *(unsigned char *)str2) 25 { 26 printf("The first string is less than the second string\n"); 27 return -1; 28 } 29 //如果循环完成,str1等于str2,那么这两个字符串相等 30 else 31 { 32 printf("Two strings are equal\n"); 33 return 0; 34 } 35 36 } 37 int main(int argc, char *argv[]) 38 { 39 char a[]="hfllo45"; 40 char b[]="hello45"; 41 42 my_strcmp(a,b); 43 44 return 0; 45 }