模拟实现strlen函数

来源:互联网 发布:类似facebook的软件 编辑:程序博客网 时间:2024/05/21 23:33

这里用三种方式模拟实现strlen函数

int my_strlen(const char *str)//指针{    char *p = str;    while (*p != '\0')    {        p++;    }    return p - str;//此时指针p指向'\0',相减即为元素个数    //if (*str == '\0')//递归    //{    //  return 0;    //}    //else    //  return 1 + my_strlen(str + 1);    //int count = 0;//借助变量    //while (*str != '\0')    //{    //  count++;    //  str++;    //}    //return count;}int main(){    char *p = "abcdef";    printf("count=%d\n", my_strlen(p));    system("pause");    return 0;}
原创粉丝点击