C语言编程函数实现前者大于后者时返回正数,小于返回负数,相等时返回0,在主函数内完成字符串输入输出

如题所述

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int my_strncmp(const char *dest, const char *sour, size_t n)
{
    assert(dest);
    assert(sour);
    while (*dest == *sour)
    {
        --n;
        if ((n == 0) || (*dest == '\0'))
            return 0;
            dest++;
            sour++;

    }
    return *dest - *sour;
}int main()
{
    char arr1[20] = "abcdefg";
    char arr2[] = "aba";
    int r;
    r = my_strncmp(arr1, arr2, 2);
    printf("%d\n", r);
    system("pause");
    return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答