当前位置:网站首页>【C语言系列】—三种方法模拟实现strlen库函数的方法
【C语言系列】—三种方法模拟实现strlen库函数的方法
2022-07-29 05:09:00 【甘城なつき】
꧁ 各位大佬们好!很荣幸能够得到您的访问,让我们一起在编程道路上任重道远!꧂
* 博客专栏:【C生万物】*
本篇内容简介:详细介绍如何使用三种方法模拟实现库函数 strlen()!
了解作者:励志成为一名编程大牛的学子,目前正在升大二的编程小白。
励志术语:编程道路的乏味,让我们一起学习变得有趣!
正文开始
文章目录
strlen的介绍以及功能
关于strlen的功能我们来看MSDN或者Cpulspuls(https://cplusplus.com/)怎么介绍的,这里我用的是MSDN。

strlen的一般用法
strlen可以求字符串的长度,即 \0 前字符的个数,看下面这个代码:
#include<stdio.h>
#include<string.h>
int main()
{
//char arr[] = "abcdef";//常量字符串 arr中存 a b c d e f \0
int len = strlen(arr);// strlen的返回值是size_t == unsigned int
printf("%d\n", len);
return 0;
}运行结果:

可以得到求出的长度为 6。
计数器法模拟实现strlen
要点:在函数中定义一个计算 \0 之前字符出现的个数,最后返回计数器计算的个数。
#include<stdio.h>
#include<assert.h>
size_t my_strlen(char* ps)
{
assert(ps);//断言这个指针
size_t count = 0;//计算个数
while (*ps)
{
count++;
ps++;
}
return count;
}
//模拟实现返回值为 size_t 类型的strlen
int main()
{
char arr[] = "abcdef";
size_t len = my_strlen(arr);
printf("%u\n", len);
return 0;
}指针 - 指针的方法模拟实现strlen
我们知道数组的地址是由低到高的,我们可以用地址的最高位减去地址的最低位,最后再返回就可以得到到这个字符的长度了。
我们来画个图:

思路理清了,就上代码:
#include<stdio.h>
#include<assert.h>
size_t my_strlen(char* src)
{
assert(src);//断言这个指针
char* dest = src;
while (*dest != '\0')
{
dest++;
}
return dest - src;
}
//模拟实现返回值为 size_t 类型的strlen
int main()
{
char arr[] = "abcdef";
size_t len = my_strlen(arr);
printf("%u\n", len);
return 0;
}递归方法模拟实现strlen
递归的方法就比较简单了,我们直接上代码吧。
#include<stdio.h>
#include<assert.h>
size_t my_strlen(char* p)
{
assert(p);//断言这个指针
if (*p == '\0')
{
return 0;
}
else
{
return 1 + my_strlen(p + 1);
}
}
//模拟实现返回值为 size_t 类型的strlen
int main()
{
char arr[] = "abcdef";
size_t len = my_strlen(arr);
printf("%u\n", len);
return 0;
}OK! 把三种模拟实现strlen的方法,都分享给大家,你会哪几种呢!
边栏推荐
- C语言求字符串的长度
- QT series - Installation
- [event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
- 直播预告:京东云DevOps与JFrog制品库的融合
- WDDM learning
- 365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
- 2022数学建模竞赛暑期培训讲座——最优化方法:目标规划
- AI应用第一课:C语言支付宝刷脸登录
- C 语言手写 QQ-AI 版
- 一维数组练习
猜你喜欢

GPIO的输入输出详解

Live broadcast Preview: integration of JD cloud Devops and jfrog product library

Arfoundation starts from zero 9-ar anchor

SM integration is as simple as before, and the steps are clear (detailed)

Live broadcast preview | how to improve enterprise immunity through "intelligent edge security"?

365天挑战LeetCode1000题——Day 038 公交站间的距离 + 基于时间的键值存储 + 转变数组后最接近目标值的数组和 + 有界数组中指定下标处的最大值

C language handwritten qq-ai version

AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带

Rimworld通过SteamCMD上传创意工坊的方法

Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse
随机推荐
OCCT学习001-----简介
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
Arfoundation starts from zero 9-ar anchor
QT series - Installation
Numpy Foundation
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
osgSimplegl3结合RenderDoc工具
Webrtc audio anti weak network technology (Part 2)
ANSI C类型限定符
Arfoundation starts from scratch 3- create an arfoundation project
APP常用跨端技术栈深入分析
分配内存:malloc()和free()
osg进阶-序
Arfoundation starts from scratch 8-geospatial API (geospatial) development
Custom QML control: imagebutton
来!看排名一年上升16位的ClickHouse,如何在京东落地实践
AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带
365天挑战LeetCode1000题——Day 035 每日一题 + 二分查找 13
AI应用第一课:C语言支付宝刷脸登录
直播预告:京东云DevOps与JFrog制品库的融合