strlen function using recursion in c

后端 未结 4 1338
离开以前
离开以前 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条回答
  •  面向向阳花
    2021-01-26 12:16

    Your problem starts here:

    i++
    

    This is called a postfix. Just use ++i or i + 1

    Postfix sends the value and just then increments the variable. It's like writing this:

    return strlen(str,i);
    i = i + 1;
    

    You have to use Prefix, which increments the variable and then sends the value. A prefix (++i) will act like that:

    i = i + 1;
    return strlen(str,i);
    

    Or just send the value without changing the variable:

    return strlen(str, i + 1);
    

    Which, in my opinion, is the simplest way to do that.

提交回复
热议问题