strlen C函数实现
#include <stdio.h>
#include <assert.h>
#include <string.h>
int my_strlen(const char* p)
{
assert(p);
int len = 0;
while (*p++)
++len;
return len;
}
int std_strlen(const char* s)
{
const char* sc;
for (sc = s; *sc != '\0'; ++sc);
return sc - s;
}
int my_strlen2(const char* p)
{
assert(p);
if (*p)
return 1+my_strlen2(p+1);
return 0;
}
int main()
{
printf("Hello, World! \n");
char *p = "Hello, World! \n";
printf("my_strlen: %d\n", my_strlen(p));
printf("my_strlen2: %d\n", my_strlen2(p));
printf("std_strlen: %lu\n", ::strlen(p));
return 0;
}