您的位置:首页 > 其它

递归和非递归分别实现strlen

2019-03-29 14:00 148 查看
//递归实现strlen
#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;
}

//非递归实现strlen
#include<stdio.h>
#include<windows.h>
int my_strlen(char *string){
 int count = 0;
 while (*string++ != '\0'){
  count++;
 }
 return count;
}
int main(){
 char *str = "abcdef";
 printf("%d\n", my_strlen(str));
 system("pause");
 return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: