需求:在不使用临时变量的情况下,利用函数求取字符串的长度
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int my_strlen(char* n ) { int i = 0; while(*n != '\0') { i++; n++; } return i; } //递归函数:大事化小 //my_strlen("ayue"); //1+my_strlen("yue"); //....... //1+1+1+0 int my_strlen2(char* n) { if (*n != '\0') { return 1+my_strlen(n+1); } return 0; } int main() { char arr[] = "ayue"; //int len = strlen(arr); //printf("%d", len); int len = my_strlen2(arr); printf("你输入的字符串长度是:%d", len); return 0; }
标签:arr,return,21,递归,int,my,练习,len,strlen From: https://www.cnblogs.com/ayue6/p/17744256.html