宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

目录

1.strlen的函数声明

2.strlen函数的头文件

3.strlen函数的使用

4.my_strlen函数的实现


1.strlen的函数声明

size_t strlen(const char *str)

2.strlen函数的头文件

#include <string.h>

3.strlen函数的使用

strlen函数时测量字符串长度的函数(不包括'\0')

#include <stdio.h>
#include <string.h>
int main()
{char arr[] = "abcdef";int len = strlen(arr);printf("%d\n", len);return 0;
}

【字符串函数】strlen的使用及原理-风君子博客

4.my_strlen函数的实现

strlen是计算一个字符串的长度,也就是字符数量,那么我们自己实现的my_strlen也遵循库函数的功能即可。

#include <stdio.h>
#include <assert.h>
int my_strlen(const char* str)
{int count = 0;assert(*str != NULL);//判断传进来的指针是否有效while (*str++)//当*str = '\0'的时候条件为假{             //   '\0'的ASCLL码值为0count++;}return count;
}
int main()
{char arr[] = "abcdef";int len = my_strlen(arr);printf("%d\n", len);return 0;
}