模拟strncmp( )函数

来源:互联网 发布:网络实名制 郭德纲 编辑:程序博客网 时间:2024/04/19 03:06

代码实现 (环境 Visual Studio 2017)

//1.模拟实现strncmp#include <stdio.h>#include <windows.h>#include <string.h>#include <assert.h>#pragma warning( disable : 4996) int My_strncmp(char * dest, const char *src, size_t n){    int ret = 0;    assert(dest);    assert(src);    while ( n && !(*dest - *src) )    {        n--;        dest++;        src++;    }    if (n && *dest - *src > 0)    {        return 1;    }    else if (n && *dest - *src < 0)    {        return -1;    }    return ret;}int main(){    char str1[20] = "123456789";    char str2[20] = "12abcde";    printf("%d\n", strncmp(str1, str2, 5)); //调用系统strncpy    printf("%d\n", My_strncmp(str1, str2, 5));//调用My_strncpy    system("pause");    return 0;}
原创粉丝点击