#include
#define N 4
int strcomp(char *s1,char *s2)
{ for(;*s1==*s2&&*s1&&*s2;s1++,s2++); /* 找不同的字符 */
return(*s1-*s2); /* 返回字符差值*/
}
void main()
{ char str[N][50];
int i,j;
for(i=0;i
gets(str[i]);
}
for(j=0,i=1;i
printf("Min string is:%s\n",str[j]);
}
3137333的程序有问题,应是:
int strcmp(char *s1, char *s2)
{
while((*s1==*s2)&&*s1) {s1++;s2++;}
return(*s1-*s2);
}
原来程序返回的是不相同字符的下一字符的差值!
#include
int strcmp(char *s1, char *s2)
{
while((*s1++ == *s2++)&& *s1);
return (*s1 - *s2);
}
void main()
{
char a[10], b[10];
gets(a);
gets(b);
printf("%d\n", strcmp(a, b));
}
strcmp源码
int __cdecl strcmp (const char *src, const char *dst)
{
int ret = 0 ;
while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
{
++src;
++dst;
}
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
int
*strcmp(const
char
*s1,
const
char
*s2)
{
for(;
*s1==*s2;
s1++,
s2++)
if(*s1
==
'\0')
return
0;
return
((unsigned
*)*s1
<
(unsigned
*)*2
?
-1
:
+1);
}