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

架构师李肯 发表于 2022/10/31 00:43:25 2022/10/31
【摘要】 你有面试中,要求写strchr的源码实现吗?本文给出一个参考写法!

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

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

1 需求说明

题目大意如下:

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

2 源码实现

2.1 函数申明

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

NAME
       strchr, strrchr, strchrnul - locate character in string

SYNOPSIS
       #include <string.h>

       char *strchr(const char *s, int c);

       char *strrchr(const char *s, int c);

       #define _GNU_SOURCE         /* See feature_test_macros(7) */
       #include <string.h>

       char *strchrnul(const char *s, int c);

DESCRIPTION
       The strchr() function returns a pointer to the first occurrence of the character c in the string s.

       The strrchr() function returns a pointer to the last occurrence of the character c in the string s.

       The strchrnul() function is like strchr() except that if c is not found in s, then it returns a pointer to the null byte at the end of s, rather
       than NULL.

       Here "character" means "byte"; these functions do not work with wide or multibyte characters.

RETURN VALUE
       The strchr() and strrchr() functions return a pointer to the matched character or NULL if the character is not found.  The terminating null byte
       is considered part of the string, so that if c is specified as '\0', these functions return a pointer to the terminator.

       The  strchrnul()  function  returns a pointer to the matched character, or a pointer to the null byte at the end of s (i.e., s+strlen(s)) if the
       character is not found.

从man中我们可以得知,strchr有很多个同系的函数,我们只要了解其中一个,就很容易掌握其他的几个。

2.2 功能实现

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

char *my_strchr(char* s, char c)
{
    while (*s && *s != c)
    	s++;
  	if (*s == c)
    	return (char *)s;
  	return NULL;
}

3 源码测试

简单的测试代码如下:

#include <stdio.h>
#include <assert.h>

int main(void)
{
    char str[30] = "123456789abcdef";
	
	printf("str-chr1: %s\r\n", strchr(str, '2'));
	printf("str-chr2: %s\r\n", my_strchr(str, '2'));
    printf("str-chr3: %s\r\n", strchr(str, 'A'));  //理应返回NULL
	printf("str-chr4: %s\r\n", my_strchr(str, 'A'));//理应返回NULL

	return 0;
}

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

image-20221031004214463

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

4 小小总结

strchr的源码实现,核心就是了解其函数原型以及如何准确地判断字符串的结束与字符的相等比较,你都get到了吗?

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华为云云享专家(嵌入式物联网架构设计师)等荣誉。坚信【知识改变命运,技术改变世界】!

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区),文章链接,文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。