递归实现:
#include <stdio.h>
#include <stdlib.h>
int Recursive_MyStrlen(char* string)
{
if (*string == '\0')
return 0;
else
{
return Recursive_MyStrlen(++string)+1;
}
}
非递归实现:
int My_Strlen(char* string)
{
int count = 0;
while (string)
{
if (string[count] == '\0')
break;
count++;
}
return count;
}
实现结果:
int main()
{
char string[] = "aehfiuh123123";
printf("%d\n", Recursive_MyStrlen(string));
printf("%d\n", My_Strlen(string));
system("pause");
return 0;
}