#include<stdio.h>
#include<string.h>
int cmp(char *,char *);
int main(void)
{
int c;
char a[10],b[10];
printf("Please enter your string.\n");
gets(a);
gets(b);
puts("The string you entered is:");
puts(a);
puts(b);
c=cmp(a,b);
if(c==1)
printf("the string \"%s\" is bigger than the string \"%s\".",a,b);
else
printf("the string \"%s\" is bigger than the string \"%s\".",b,a);
printf("%d",c);
return 0;
}
int cmp(char *ptr1,char *ptr2)
{
int flag;
if(strlen(ptr1)!=strlen(ptr2))
{
flag=(strlen(ptr1)>strlen(ptr2))?1:-1;
return flag;
}
else
{
while((*ptr1!='\0') && (*ptr2!='\0'))
{
if(*ptr1>*ptr2)
{
flag=1;
return flag;
}
else if(*ptr1<*ptr2)
{
flag=-1;
return flag;
}
else
{
ptr1++;
ptr2++;
}
}
return -1;
}
}
我写的一个,大家有空批一下啊