比较两个字符串大小,并输出它们首个不相同字符的ASCII码差值

2024-11-19 09:26:31
推荐回答(2个)
回答1:

#include
#include
main()
{char str1[100],str2[100];
int i=0;
printf("enter the first string\n");
scanf("%s",str1);
printf("enter the second string\n");
scanf("%s",str2);
while(str1[i]!='\0' &&str2[i]!='\0' )
{ if( str1[i]!= str2[i] ) break;
else i++;}
printf("%d \n",str2[i]-str1[i]);
}

回答2:

咯咯

#include //数组中字符串比较(指针)
int cmp(char a[],char b[]);
main()
{
char a[100]="giggle",b[100]="gigglf";
printf("%d",cmp(a,b));
printf("\n");
}
int cmp(char *p,char *q)
{
while(*p==*q&&*p&&*q){p++;q++;}
return *p-*q;
}