当前位置: 首页 > news >正文

c语言:模拟strlen(三种方法)最全版本

1.计数的方法

#include <stdio.h>
#include <assert.h>
int my_strlen(const char * str)//const的使用优化
{int count=0;assert(str)while(*str){count++;str++;}return count;
}

2.用指针的方法(指针-指针)

#include <stdio.h>
#include <assert.h>
int my_strlen(char * s )
{assert(s);char* p = s;while (*p != '\0'){p++;}return p - s;
}

 3.不创建临时变量的方法(递归的方法)

#include <stdio.h>
#include <assert.h>
int my_strlen(char *p)
{assert(p);if(*p=='\0')return 0;elsereturn 1+my_strlen(p+1);
}

函数的调用

int main()
{char arr[]="abcdef";size_t len=my_strlen;printf("%zd\n",len);return 0;
}

运行结果:

http://www.lryc.cn/news/354179.html

相关文章:

  • 线性模型--普通最小二乘法
  • 移动云以深度融合之服务,令“大”智慧贯穿云端
  • 簡述vue常用指令
  • 【建议收藏】用AI快速生成一个网页(名侦探柯南~灰原哀主题网页),适合大学生web期末大作业
  • 用c++用4个凸函数(觉得啥好用用啥)去测试adam,rmsprop,adagrad算法的性能(谁先找到最优点)
  • AJAX初级
  • 重载大于号运算符,比较复数大小
  • go ast语义分析实现指标计算器
  • 【Vue】组件间传参与方法调用
  • 类和对象2
  • Linux系统命令traceroute详解(语法、选项、原理和实例)
  • 中兴通讯助力中国移动,推动SPN AI节能技术于23省规模部署
  • SQL Server--死锁
  • 中科蓝讯AB32VG1中文寄存器说明GPIO端口操作
  • 如何查看热门GPT应用?
  • C++中的各种定义
  • Java面向对象-常用类(日期时间类)
  • Shell环境变量深入:自定义系统环境变量
  • 【C++课程学习】:命名空间的理解(图文详解)
  • 鸿蒙ArkUI-X平台差异化:【运行态差异化(@ohos.deviceInfo)】
  • 蓝牙Mesh模块组网时无线回程影响速率吗?
  • 将3D检测的box框投影到BEV图片上
  • Flutter 中的 ClipOval 小部件:全面指南
  • ubuntu 硬盘转移
  • three.js中使用CameraHelper来可视化调整阴影相机的范围
  • Golang发送GET请求并设置查询参数
  • c++笔记3
  • 唠唠叨叨,每日进度
  • Vulhub——CAS 4.1、AppWeb、apisix
  • Python Beautiful Soup 使用详解