求字符串长度的三种方式

时间:2022-07-13 00:40:44
#include<stdio.h>
#include<string.h>

int len_c1(char* arr) { //自定义函数(无递归)
int num = 0;
while (*arr != '\0') {
num++;
arr++;
}
return num;
}

int len_c2(char* arr) { //自定义函数(递归)
while (*arr != '\0') {
return 1 + len_c2(arr + 1);
}
return 0;
}

int main() {
char arr[] = "woshizhendeaini";
printf("字符串长度为%d\n", len_c1(arr));
printf("字符串长度为%d\n", len_c2(arr));
printf("字符串长度为%d\n", (unsigned int)strlen(arr));
return 0;
}