C语言问题:不用strcmp函数比较两个字符串的大小

来源:百度知道 编辑:UC知道 时间:2024/06/15 13:15:56
#include<stdio.h>
main()
{ int i=0;
char a[100],b[100];
gets(a);gets(b);
while(a[i] == b[i]&&a[i]!='\0')i++;
if (a[i] == '\0')
printf("The 2 strings are the same.");
else
printf("The 2 strings are different.");
}

这个程序只能比较两个字符串是否相同 如果改这个程序 使它能比较出字符串a>b a<b a=b 三种情况呢?

#include<stdio.h>
int main()
{
int i=0;
char a[100],b[100];
gets(a);gets(b);
while(a[i] == b[i]&&a[i]!='\0')i++;
if (a[i] == '\0'&&b[i]=='\0')
printf("The 2 strings are the same. a =b\n");
else {
if(a[i] > b[i])
printf("The 2 strings are different. a > b\n");
else
printf("The 2 strings are different. a < b\n");
}
}

做减法可以比较啊