#include <stdio.h>
#include <stdlib.h>
//实现strlen
int my_strlen1(char str[])//数组
{
int count = 0;
int i = 0;
while (str [i] != ‘\0‘)
{
count++;
i++;
}
return count;
}
int my_strlen2(char *str)//指针
{
int i = 0;
while (*str )
{
i++;
str++;
}
return i;
}
int my_strlen3(char *str)//递归
{
int i = 0;
if (*str == ‘\0‘)
{
return 1;
}
return 1 + my_strlen3(str + 1);
}
int main()
{
char str[] = "abcdef" ;
int ret = my_strlen1(str);
printf( "%s的长度为%d\n" , str, ret);
int ret2 = my_strlen2(str);
printf( "%s的长度为%d\n" , str, ret2);
int ret3 = my_strlen3(str);
printf( "%s的长度为%d\n" , str, ret3);
system( "pause");
return 0;
时间: 2024-11-04 02:20:54