字符串比较函数:比较的是对应位置的ASCII值
int strcmp ( const char * str1, const char * str2 );
标准规定:
#include
int main()
{
char str[] = "hel";
char arr[20] = "hello ";
int ret = strcmp(arr, str);
printf("%d", ret);
return 0;
}
vs环境下得出的值是1,不同环境下得出的具体数值不同,但都是正数
#include
#include
int my_strcmp(const char* dest, const char* src)
{
assert(dest != NULL);
assert(src != NULL);
while (*dest == *src)
{
if (*dest == '\0')
return 0;
dest++;
src++;
}
if (*dest > *src)
{
return 1;
}
else
{
return -1;
}
}
int main()
{
char str[] = "hel";
char arr[20] = "hello ";
int ret = my_strcmp(arr, str);
printf("%d", ret);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int my_strcmp(const char* src, const char* dst)
{
int ret = 0;
assert(src != NULL);
assert(dst != NULL);
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 main()
{
char str[] = "hel";
char arr[20] = "hello ";
int ret = my_strcmp(arr, str);
printf("%d", ret);
return 0;
}