要求:不能使用局部变量和全局变量,实现strlen功能
思路:用递归实现,递归出口条件为*str=='\0'
int strlen(char* str)
{
assert(str);
return *str=='\0' ? 0 : 1+strlen(str+1);
}
方法二:
可使用局部变量
#include <stdio.h>
#include <assert.h>
int strlen(const char* src)
{
assert(src != NULL);
int len = 0;
while((*src++) != '\0')//while(*src++)
{
len++;
}
return len;
}