#include <stdio.h>
#include <stdlib.h>
第一种方法:
int Mystrlen(const char* str )
{
if (str == NULL)
{
return -1;
}
if (*str == ‘\0’)
{
return 0;
}
int size = 0;
while (str != ‘\0’)
{
++size;
++str;
}
return size;
}
第二种方法:
int Mystrlen(const char str) //递归
{
if (str == NULL)
{
return -1;
}
if (*str == ‘\0’)
{
return 0;
}
return 1 + Mystrlen(str+1) ;
}
int main()
{
char source[] = “hello”;
int size=Mystrlen(source);
printf("%d\n", size);
system(“pause”);
return 0;
}