功能:比较字符串str1 and str2, 返回值如下:
less than 0
str1 is less than str2
equal to 0
str1 is equal to str2
greater than 0
str1 is greater than str2
例如: printf( "Enter your name: " );
scanf( "%s", name );
if( strcmp( name, "Mary" ) == 0 )
printf( "Hello, Dr. Mary!\n" );
int strcmp(char *str1, char*str2)
{
while (*str1 != '\0' || *str2 != '\0')
{
if (*str1 > *str2)
{
return 1;
}
else if (*str1 == *str2) //判断相同往后继续比较
{
str1++;
str2++;
}
else
{
return -1;
}
}
if (*str1 == '\0' || *str2 == '\0') //比较到最后
{
return 0;
}
}