用递归编写strlen函数

不用库函数,以及任意变量,编写strlen函数


int my_strlen(const char *str)

{

    assert(NULL != str);

    if('\0' == *str)

{

    return 0;

}

else

{

    return (1+my_strlen(++str) );

}

}


#include <stdio.h>
#include <assert.h>


int my_strlen(const char *str)
{
assert(NULL != str);
return ('\0' != *str)? (1+my_strlen(str+1)) : 0;

}


int main()
{
char str[100] = "12345";
printf("%d\n",my_strlen(str) );
}

猜你喜欢

转载自blog.csdn.net/yinzewen123/article/details/80789156
今日推荐