#include
int strcmp(char *p1,char *p2)
{
while(*p1||*p2)
{
if(*p1!=*p2)
return(*p1-*p2);
else
{
p1++;
p2++;
}
}
return 0;
}
void main()
{
char s1[20],s2[20],*p1,*p2; //字符串
int k;
printf("请键入两个字符串:\n");
scanf("%s %s",s1,s2);
p1=s1;
p2=s2;
k=strcmp(p1,p2);
printf("Result is:%d\n",k);
}
修改如下:
#include
void main()
{
int strcmp(char *s1,char *s2);
char str1[30],str2[30];
printf("\n please input string1:");
gets(str1);
printf("\n please input string2:");
gets(str2);
printf("%d\n",strcmp(str1,str2));
}
int strcmp(char *s1,char *s2)
{
int i=0;
while (s1[i]&&s2[i]&&s1[i]==s2[i])
i++;
return s1[i]-s2[i];
}
以上回答你满意么?