
strlen()函数的模拟实现
在c语言中,strlen()函数储存在string.h头文件中,作用是计算一个字符串的字符数。
实际上,这个函数内部运行中检测到 则停止运行,之后返回字符数。
下面我们通过三种方法模拟实现该函数的功能。
1.计数器法
代码如下
#include <stdio.h>
#include <assert.h>
int myStrlen(const char *p)
{assert(p != NULL);int count = 0; //开始假设字符串中字符数为0while (*p != '