废话不多说直接上代码
#include <stdio.h>
#include <stdlib.h>
int Mystrcmp(char *str1,char *str2){
if(str1==NULL ||str2==NULL){
printf("str1或str2中有野指针");
exit(-1);
}
int value=0;
while(*str1!='\0' && *str2!='\0'){
if(*str1!=*str2){
value=value+(*str1-*str2);
}
str1++;
str2++;
}
if(value>0){
value=1;
}
if(value<0){
value=-1;
}
return value;
}
int main(){
char *str1="hbppyd";
char *str2="happyc";
int i = Mystrcmp(str1,str2);
printf("%d\n",i);
return 0;
}
这里直接比较的是字符串的整体大小,而C语言系统给的是直接比较第一个不同字符的大小