int My_strncmp(const char* str1, const char* str2, int count)
{
assert((str1 != NULL) &&( str2 != NULL));
if (NULL == str1 || NULL == str2)
return -2;
// 1.两个值不相等;2,相等但是是'\0';3,需要比较的个数count 已经比较完毕
// 如果写成count-- 则会让循环体代码多执行一次
while (--count && (*str1 == *str2) && (*str1 != '\0') && (*str2 != '\0'))
{
str1++;
str2++;
}
int tmp = *str1 - *str2;
if (tmp == 0)
{
return 0;
}
else if (tmp > 0)
{
return 1;
}
else
{
return -1;
}
}
int main()
{
const char str1[] = "abcde";
const char str2[] = "abcf";
int temp = My_strncmp(str1,str2,4);
printf("%d\n", temp);
return 0;
}