#include <stdio.h>
int main()
{
int stringcmp(char *,char *);
int m;
char str1[80],str2[80],*p1,*p2;
scanf("%s",str1);
scanf("%s",str2);
p1=&str1[0];
p2=&str2[0];
m=stringcmp(p1,p2);
printf("%d",m);
return 0;
}
int stringcmp(char *p1,char *p2)//指针指向数组所在的地址
{
while(*p1==*p2&&*p1!='\0'&&*p2!='\0')//规定数组中最后不为'\0'以防止数组越界
{
p1++;
p2++;
}
return (*p1-*p2);
}
Description
写一函数,实现两个字符串的比较。即自己写一个strcmp函数,函数原型为
int stringcmp(char *p1,char *p2);
设p1指向字符串s1,p2指向字符串s2。要求当s1=s2时,返回值为0,若s1≠s2,返回它们二者第1个不同字符的ASCII码差值(如"BOY"与"BAD",第2个字母不同,"O"与"A"之差为79-65=14)。如果s1>s2,则输出正值,如s1<s2,则输出负值。
Input
两个字符串
Output
比较结果
Sample Input
BOY
BAD
Sample Output
14