C++ recursion | SoloLearn: Learn to code for FREE!

+10

C++ recursion

I was seeing the recursion course of leetcode and then they reversed a string using recursion but can't understand how please explain this code Here is the code: https://code.sololearn.com/cJRt5NwXWtFt/?ref=app

c++ c

9/16/2020 12:06:37 PM

Gorom_Pani

6 Answers

New Answer

+8

The if conditon is checking if the pointer is having no char value (null or something falsey) then recursion will stop! (as the return statement will not allow to execute the code after it) Else, It is calling for the recursion! It will go to the next character until it reaches the last character! Then the recursion will stop! 1. Let's check when the next character to the last character is the input: The if statement will be executed and recursion will stop! Now, let's go backwards! Then, 2.The last character (which called the 1.), will print itself! Hence we see an 'e' 3. Then the character before that (which called 2.) will print itself after the '2.' is executed and hence it will show 'l' (second last character) This will go on until we reach the original function (first called). And hence it will print the first character after this long recursion! I hope you know that adding a pointer with one means going to the next char in the string! (Here, "hello") I hope this clears your doubts! Happy coding!

+6

AteFish🇧🇩 Isn't my first answer enough? See that the cout statement is after the calling of the recursion If the cout statement was before the calling of rev, then it would have shown 'Hello' So, 1. In the first call, It will call rev('e') and then print 'h'. 2. Same in the second call, It will call rev('l') and then print 'e' ........ 3. In the second last call, It will call rev('') and then print 'o' 4. In the last call It will return nothing (useless) So, we will see: '' => last call 'o' => after calling the 4., the 3. will print 'o' (see point 3) And so on till, 'h' => after calling 2. (Which prints 'e'), the 1. will print 'h'. And hence we get, olleh

+5

AteFish🇧🇩 😑 You don't need an else statement The code ahead the return statement will not execute Applying the else statement is optional! int hello() { return 5; cout << 10; } Here, 10 will not be printed

+3

Namit Jain please write how the program reverses hello step by step

+2

Namit Jain But I haven't added else statement for which the last falsy or null may be executed

+1

AteFish🇧🇩 when return statement is executed after null is encountered,the function returns back to where it was called from so the last value with which function was called was rev(s+1), Where s* pointing to o and s is o as well now it executes the next statement after rev(s+1) cout<<*s; which prints "o" then again function returns back to previous state where in rev(s+1) s was lo and s* pointing to l And next statement is executed again printing "l" And so we get "olleh" Here is a link to recursion understanding (in python)in which there is a diagram that might help you understanding how function returns back to the first most function call when return is encountered https://stackabuse.com/understanding-recursive-functions-with-python