C语言第四十一弹--模拟实现strlen

简介: C语言第四十一弹--模拟实现strlen

使用C语言模拟实现strlen

源码及作用: int Strlen(const char * s),求字符串长度。

思路:已知字符串储存在数组中,并默认’\0’结尾,那么使用’\0’作为循环判断条件,循环一次长度增加,循环结束,就可以得到字符串长度。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int my_strlen(const char*  str)
{
  int count = 0;
  while (*(str) != '\0')
  {
    count++;
    str++;
  }
  return count;
}
int main()
{
  char arr[] = "abcdef";
  int ret = my_strlen(arr);
  printf("%d",ret);
  return 0;
}
相关文章
|
4月前
|
Serverless 编译器 C语言
【C语言】指针篇- 深度解析Sizeof和Strlen:热门面试题探究(5/5)
【C语言】指针篇- 深度解析Sizeof和Strlen:热门面试题探究(5/5)
|
8月前
|
C语言
【C语言基础篇】字符串处理函数(一)strlen的介绍及模拟实现
【C语言基础篇】字符串处理函数(一)strlen的介绍及模拟实现
|
8月前
|
C语言
C语言学习记录——模拟字符串相关函数(strcpy、strlen、strcat)相关知识-const、typedef
C语言学习记录——模拟字符串相关函数(strcpy、strlen、strcat)相关知识-const、typedef
46 1
|
8月前
|
C语言
C语言——oj刷题——模拟实现库函数strlen
C语言——oj刷题——模拟实现库函数strlen
55 0
|
8月前
|
C语言
C 语言关于sizeof() 和 strlen()区别?
C 语言关于sizeof() 和 strlen()区别?
57 2
|
9月前
|
C语言
C语言(8)----长度计算方法:sizeof与strlen的对比
C语言(8)----长度计算方法:sizeof与strlen的对比
52 0
|
9月前
|
C语言
C语言进阶⑬(字符串函数)+(指针编程题)strlen+strcpy+strcat+strstr+strtok+strerror(下)
C语言进阶⑬(字符串函数)+(指针编程题)strlen+strcpy+strcat+strstr+strtok+strerror
53 0
|
9月前
|
安全 C语言
C语言进阶⑬(字符串函数)+(指针编程题)strlen+strcpy+strcat+strstr+strtok+strerror(中)
C语言进阶⑬(字符串函数)+(指针编程题)strlen+strcpy+strcat+strstr+strtok+strerror
54 0
|
9月前
|
C语言
C语言进阶⑬(字符串函数)+(指针编程题)strlen+strcpy+strcat+strstr+strtok+strerror(上)
C语言进阶⑬(字符串函数)+(指针编程题)strlen+strcpy+strcat+strstr+strtok+strerror
61 0
|
9月前
|
程序员 编译器 测试技术
C语言初阶⑨(调试)(如何写出好的代码)(模拟实现strcpy和strlen)
C语言初阶⑨(调试)(如何写出好的代码)(模拟实现strcpy和strlen)
72 1

热门文章

最新文章