strlen函数是一个求字符串长度的函数,返回值字符串的长度。
重写strlen就是不调用头文件#include<string.h>中的strlen函数求字符串长度。
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int my_strlen(const char *msg)
{
assert(msg);
const char *start = msg;
while (*start != '\0')
{
start++;
}
return start - msg;
}
int main()
{
char arr[] = "abcdefg";
int res = my_strlen(arr);
printf("the length of string is:>%d\n", res);
system("pause");
return 0;
}