递归和非递归分别实现strlen

来源:互联网 发布:mysql如何查看表 编辑:程序博客网 时间:2024/05/19 22:01

递归:

#include<stdio.h>#include<windows.h>int my_strlen(char *string){    int count = 0;    if (*string != '\0')    {        string++;        count = 1 + my_strlen(string);    }    return count;}int main(){    char *str = "abcdef";    printf("%d\n",my_strlen(str));    system("pause");    return 0;}

非递归:

#include<stdio.h>#include<windows.h>int my_strlen(char *string){    int count = 0;    if (*string != '\0')    {        count++;    }    return count;}int main(){    char *str = "abcdef";    printf("%d\n",my_strlen(str));    system("pause");    return 0;}
阅读全文
0 0
原创粉丝点击