当前位置:网站首页>三种方法模拟实现库函数strlen,加深对strlen的理解
三种方法模拟实现库函数strlen,加深对strlen的理解
2022-07-13 19:14:00 【别时须清欢】
一.对库函数strlen的理解
在cplusplus网站上搜索strlen
通过对在网站上对strlen的检索可以知道,strlen以下信息
1.这是一个只负责字符串的库函数!
2.计算的是字符串 \0 之前的长度,不包括\0
3.传入的参数只能是指针!
4.返回的类型是size_t类型,本质是unsigned int类型
二.模拟实现库函数strlen
1.计数器法
//1.计数器法
//将字符串的地址传入,找到字符串中每一个不为零的字符并统计
#include<assert.h>
#include<stdio.h>
#include<string.h>
//防止源头字符串被修改,用const修饰
size_t my_strlen(const char* str)
{
assert(str);//断言传入为非空指针
int count = 0;//计数器
while (*str++) //不为零的字符
{
count++;
}
return count;
}
int main()
{
char arr[] = "abcdef";
size_t len=my_strlen(arr);
printf("%u", len);//由于是模拟strlen,返回类型是size_t,因此用%u打印
return 0;
}
2.递归法
//2.递归法
#include<assert.h>
#include<stdio.h>
size_t my_strlen(const char* str)
{
assert(str);//断言传入为非空指针
if (*str == '\0') //控制条件,直到找到了 \0 停止递归
return 0;
else //第一个字符不是\0,长度加 1,并向下找下一个字符
return 1 + my_strlen(str + 1);
}
int main()
{
char arr[] = "abcdef";
size_t len = my_strlen(arr); //size_t 本质为unsigned int类型
printf("%u", len);
return 0;
}
3.指针法
//3.指针相减法
//找到字符串起始位置和\0的位置
//指针与指针相减为中间元素个数
#include<assert.h>
#include<stdio.h>
size_t my_strlen(const char* str)
{
assert(str);//断言传入的str为非空指针
char* p = str;//记录起始位置
//寻找\0得位置
while (*p)
{
p++;
}
return p-str; //指针相减为中间元素得个数
}
int main()
{
char arr[] = "abcdef";
size_t len = my_strlen(arr);
printf("%u", len);
return 0;
}
三.strlen的一些特殊情况
1.字符串中间添加 \0 相当于截断字符串 \0后面的内容
#include<assert.h>
size_t my_strlen(const char* str)
{
assert(str);
char* p = str;//记录起始位置
//寻找\0得位置
while (*p)
{
p++;
}
return p-str; //指针相减为中间元素得个数
}
#include<stdio.h>
int main()
{
char arr[] = "abc\0ef";
size_t len = my_strlen(arr);
printf("%u", len);
return 0;
}
运行结果为:
边栏推荐
- Quick news: Youxian responded to the closure of 9 cities in 3 days every day; Mingchuangyou products will be listed on the Hong Kong Stock Exchange
- 快讯:每日优鲜回应3天关闭9城业务;名创优品将在港交所上市
- HCIP第四天实验
- Hcip first day notes
- qt之QString正则表达式
- [ES6] let, const keywords and deconstruction assignment
- 渲染流程,代码是如何变为页面的(二)
- Is it difficult to become a hardware engineer?
- Response. Write specific introduction
- 第三讲:取硬币
猜你喜欢
![[go] II. Introduction to restful API, API process and code structure](/img/fd/8ae3d6a4c0d0c973ce81672c1c529c.png)
[go] II. Introduction to restful API, API process and code structure
![[go] Ⅱ. Introduction à l'API reposante et au processus et à la structure du Code de l'API](/img/fd/8ae3d6a4c0d0c973ce81672c1c529c.png)
[go] Ⅱ. Introduction à l'API reposante et au processus et à la structure du Code de l'API

EMQX Cloud 更新:新增 Redis 和 JWT 外部认证授权

Hcip first day notes

【虹科技术】网络万用表在数据中心的应用

Emqx cloud update: add redis and JWT external authentication authorization

Hcip third day notes

容错、熔断的使用与扩展

Left leaning heap - Analysis and Implementation

QT+VS 工程在 Release/Debug 文件夹下导入相关 DLL (非常实用)
随机推荐
js中树形结构的深度遍历与广度遍历
什么是Restful风格?与传统的访问风格有什么不同?
[go] Ⅱ. Introduction à l'API reposante et au processus et à la structure du Code de l'API
Hcip day 5 experiment
HCIP静态路由
Password key hard coding check
EMQX Cloud 更新:新增 Redis 和 JWT 外部认证授权
HCIP第五天笔记
【IDEA】check out master invalid path 问题
What if the win11 touchpad doesn't work? The solution of win11 touch panel not working
Various methods of obtaining form handle in VC
Web security - DOS regular expression denial of service attack
Programmer transformation project management? Get a PMP?
【每日一题】735. 行星碰撞
Judge whether two binary trees are isomorphic, and three implementation methods (recursion, queue, stack)
第三讲:取硬币
Extract the language you need from the multilingual data set and save it as CSV
Virtualization path of GPU resource pool
管理学的发展
Jerry's dot matrix screen displays the Chinese Bluetooth name [article]