#include
#include
int main()
{
int i,result;
char s1[15],s2[10];
printf("please input string1:\n");
gets(s1);
printf("please input string2:\n");
gets(s2);
i=0;
while(s1[i]==s2[i]&&s1!='\0') i++;//当s1、s2相同字符时且s1!='\0'时进入循环
if(s1[i]=='\0'&&s2=='\0')//s1、s2所有字符比较完毕都相等
result = 0;
else
result =s1[i]-s2[i];//s1、s2某个字符不相等,输出结果
printf("The result of comparing two strings is %d\n",result);
return 0;
}
#include
int strcompare(char *p1,char *p2)
{
int i;
for(i=0;p1[i]!='\0'&&p2[i]!='\0';i++)
if(p1[i]!=p2[i])//两个字符串长度相同时,其中某个字符不同
return (p1[i]-p2[i]);
if(p1[i]=='\0'&&p2[i]=='\0')//两个字符串长度相同时且所有字符相同
return 0;
else
return (p1[i]-p2[i]);//两个字符串长度不相等且某个字符不同
}
int main()
{
char s1[15],s2[10];
printf("please input string1:\n");
gets(s1);
printf("please input string2:\n");
gets(s2);
printf("The result of comparing two strings is %d\n",strcompare(s1,s2));
return 0;
}