法一:
#include<stdio.h>
int my_strlen(char* str)
{
int count = 0;
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
int len = my_strlen("abc");//char arr[] = { "abc" };
printf("%d\n", len); //int len = my_strlen(arr);
}
法二:利用指向‘\0’的指针和初始位置指针作差求元素个数
#include<stdio.h>
int my_strlen(char* str)
{
char* start = str;
while (*str != '\0')
{
str++;
}
return str-start;
}
int main()
{
int len = my_strlen("abc");//char arr[] = { "abc" };
printf("%d\n", len); //int len = my_strlen(arr);
}