Home > Article > Backend Development > [C language] Recursive and non-recursive implementations of strlen respectively
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int Strlen1(char* str) {//递归 if (*str == '\0') { return 0; } else { return Strlen1(str + 1) + 1; } } //************ int Strlen2(char* str) {//非递归 int n = 0; while (*str != '\0') { ++str; ++n; } return n; } void main() { char str[30] = { 0 }; printf("请输入一串字符\n"); scanf("%s", &str); printf("递归判断字符串长度是:%d\n", Strlen1(str)); printf("非递归判断字符串长度是:%d\n", Strlen2(str)); system("pause"); }
【推荐课程:C视频教程】
The above is the detailed content of [C language] Recursive and non-recursive implementations of strlen respectively. For more information, please follow other related articles on the PHP Chinese website!