Description
编写函数strcomp(char *s1, char *s2),实现两个字符串的比较,返回值为1、0或-1,分别表示s1>s2,s1=s2,s1
Input
多组测试数据,每组输入两个字符串(字符串长度小于80)。
Output
根据字符串的大小关系,输出1、0或-1
#include<stdio.h>
int strcmp(char *str1,char *str2)
{
if(str1!=NULL&&str2!=NULL)
{
while(*str1++ !='\0' && *str2++ !='\0')
{
if(*str1>*str2)
return 1;
else if(*str1<*str2)
return -1;
else
continue;
}
if((*str1 == '\0') && (*str2 == '\0'))
return 0;
else if((*str1 == '\0') && (*str2 != '\0'))
return -1;
else
return 0;
}
}
int main()
{
char a[85],b[85];
while(gets(a)!=NULL)
{
gets(b);
printf("%d\n",strcmp(a,b));
}
return 0;
}