C语言 字符串比较不用strcmp函数

保留strcmp的比较规则

字符串比较函数strcmp的比较,两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。基本形式为strcmp(str1,str2),若str1=str2,则返回零;若str1str2,则返回正数。

#include
#include

int main()
{
    char a[20],b[20];
    int i=0,k=233;
    unsigned long m,n;
    scanf("%s",a);
    scanf("%s",b);
    m=strlen(a);
    n=strlen(b);
    while (a[i]!='\0'&&a[i]==b[i]) {
        i++;
    }
    if (a[i]=='\0'&&b[i]=='\0') {
        printf("0");
        return 0;
    }
    else{
        k=a[i]-b[i];
    }
    if (k>0) {
        printf("1");
    }
    else printf("-1");
    
    
    return 0;
}

你可能感兴趣的:(c语言,蓝桥杯,开发语言)