C語言學習第八天【函數遞歸】ide
【例題一】使用函數遞歸求字符串長度函數
#define _CRT_SECURE_NO_WARNINGS
學習
#include <stdio.h>spa
#include <string.h> 遞歸
int my_strlen(char* str) {字符串
int count = 0;string
while (*str != '\0') {it
count++;io
str++;class
}
return count;
}
int main() {
char arr[] = "bit";
int len = my_strlen(arr);
printf("len = %d\n", len);
return 0;
}
【例題二】使用函數遞歸打印整數的單個數值
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void print(int n) {
if (n > 9) {
print(n / 10);
}
printf(" %d ", n%10);
}
int main() {
unsigned int sum = 0;
scanf("%d", &sum);
print(sum);
return 0;
}