strlen function using recursion in c

后端 未结 4 1371
离开以前
离开以前 2021-01-26 11:17

I\'m kida new to the recursion subject and i\'ve been trying to write the \"strlen\" function using recurion, thats what i tried:

int strlen ( char str[], int i         


        
4条回答
  •  Happy的楠姐
    2021-01-26 11:59

    size_t strlen (char* str) {
        if (*str == 0) {
            return 0;
        }
    
        return strlen (str+1) +1;
    }
    

    So :

    • strlen ("") == 0
    • strlen ("a") -> strln("") + 1 == 1
    • strlen ("he") -> strln("e") + 1) = (strln("") + 1) + 1 == 2

    etc

提交回复
热议问题