首页 > 行业资讯 > 正文

【C语言经典面试题】源码实现标准库函数strncmp

你有面试中,要求写strncmp的源码实现吗?本文给出一个参考写法!

1 需求说明2 源码实现2.1 函数申明2.2 功能实现3 源码测试4 小小总结5 更多分享

1 需求说明

题目大意如下:

请参考标准C库对strncmp的申明定义,使用C语言的语法写出其实现源码。

2 源码实现

2.1 函数申明

通过查看man帮助,我们可以知道strncmp函数的功能及其简要申明。

复制NAME strcmp, strncmp – compare two strings ​ SYNOPSIS #includeint strcmp(const char *s1, const char *s2); ​ int strncmp(const char *s1, const char *s2, size_t n); ​ DESCRIPTION The strcmp() function compares the two strings s1 and s2. The locale is not taken into account (for a locale-aware comparison, see strcoll(3)). It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. ​ The strncmp() function is similar, except it compares only the first (at most) n bytes of s1 and s2. ​ RETURN VALUE The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

2.2 功能实现

以下是我的一个简单实现源码,仅供参考:

复制int my_strncmp(const char *s1, const char *s2, size_t n) { if (n == 0) return 0; ​ while (n– != 0 && *s1 == *s2) { if (n == 0 || *s1 == ) break; s1++; s2++; } ​ return (*(unsigned char *) s1) – (*(unsigned char *) s2); } 3 源码测试

简单的测试代码如下:

复制​ #include #include #include ​ static int my_strcmp(const char *s1, const char *s2) { while (*s1 != && *s1 == *s2) { s1++; s2++; } ​ return (*(unsigned char *)s1) – (*(unsigned char *)s2); } ​ static int my_strncmp(const char *s1, const char *s2, size_t n) { if (n == 0) return 0; ​ while (n– != 0 && *s1 == *s2) { if (n == 0 || *s1 == ) break; s1++; s2++; } ​ return (*(unsigned char *) s1) – (*(unsigned char *) s2); } ​ int main(int argc, const char *argv[]) { char s0[10] = “123”; char s1[10] = “123”; char s2[] = “hello”; char s3[] = “hello1234567890”; char s4[] = “01023”; ​ printf(“strcmp => %d “, strcmp(s0, s1)); printf(“my_strcmp => %d “, my_strcmp(s1, s0)); ​ printf(“strcmp => %d “, strcmp(s0, s2)); printf(“my_strcmp => %d “, my_strcmp(s1, s2)); ​ printf(“strcmp => %d “, strcmp(s0, s4)); printf(“my_strcmp => %d “, my_strcmp(s1, s4)); ​ printf(“strcmp => %d “, strcmp(s2, s3)); printf(“my_strcmp => %d “, my_strcmp(s2, s3)); ​ printf(“————————— “); ​ printf(“strncmp => %d “, strncmp(s0, s1, 3)); printf(“my_strncmp => %d “, my_strncmp(s1, s0, 3)); ​ printf(“strncmp => %d “, strncmp(s0, s2, 5)); printf(“my_strncmp => %d “, my_strncmp(s1, s2, 5)); ​ printf(“strncmp => %d “, strncmp(s0, s4, 4)); printf(“my_strncmp => %d “, my_strncmp(s1, s4, 4)); ​ printf(“strncmp => %d “, strncmp(s2, s3, 5)); printf(“my_strncmp => %d “, my_strncmp(s2, s3, 5)); ​ return 0; }

执行编译后,运行小程序的结果:

【C语言经典面试题】源码实现标准库函数strncmp-c标准库random安全吗

从运行结果上看,基本满足了题目要求,有心的读者可以进一步测试其他测试用例。

4 小小总结 strncmp就是执行字符串比较,关键还是要识别字符串的结束; 示例代码中,很巧妙地利用while中的判断,简单粗暴就达到了判断字符串结束的目的; 函数的返回值也是有特殊含义,切勿搞错;总共有三种情况,大于0、小于0、等于0;其中等于0表示两个字符串相等(一模一样); 注意其与strcmp的区别,有些不相等的两个字符串,通过n参数输入的不同,也可能返回0( 字符串相等 )。 5 更多分享

[架构师李肯]

架构师李肯全网同名 ),一个专注于嵌入式IoT领域的架构师。有着近10年的嵌入式一线开发经验,深耕IoT领域多年,熟知IoT领域的业务发展,深度掌握IoT领域的相关技术栈,包括但不限于主流RTOS内核的实现及其移植、硬件驱动移植开发、网络通讯协议开发、编译构建原理及其实现、底层汇编及编译原理、编译优化及代码重构、主流IoT云平台的对接、嵌入式IoT系统的架构设计等等。拥有多项IoT领域的发明专利,热衷于技术分享,有多年撰写技术博客的经验积累,连续多月获得RT-Thread官方技术社区原创技术博文优秀奖,荣获[CSDN博客专家]、[CSDN物联网领域优质创作者]、[2021年度CSDN&RT-Thread技术社区之星]、[2022年RT-Thread全球技术大会讲师]、[RT-Thread官方嵌入式开源社区认证专家、[RT-Thread 2021年度论坛之星TOP4]、[华为云云享专家(嵌入式物联网架构设计师)]等荣誉。坚信【知识改变命运,技术改变世界】!

审核编辑:汤梓红

猜你喜欢