使用递归和非递归分别实现 strlen

it2022-05-05  136

递归实现:

#include <stdio.h> #include <stdlib.h> int Recursive_MyStrlen(char* string) { if (*string == '\0') return 0; else { return Recursive_MyStrlen(++string)+1; } }

非递归实现:

int My_Strlen(char* string) { int count = 0; while (string) { if (string[count] == '\0') break; count++; } return count; }

实现结果:

int main() { char string[] = "aehfiuh123123"; printf("%d\n", Recursive_MyStrlen(string)); printf("%d\n", My_Strlen(string)); system("pause"); return 0; }


最新回复(0)