please send me the code for above que. i have not understand the code on gfg . tell me the diffrent approach.
Que ->reverse a string using recursion
@ankit_verma in recursion, you have to follow the following approach:
divide problem into smaller subproblems, apply recursion to solve the smaller problem and assume that recursion will solve it automatically(ie you dont have to worry about the smaller problem, just worry about the problem at hand)
figure out a base case, to stop the recursion
combine the answer for smaller problem with the current(bigger) problem and return the answer
here: problem is to reverse a string
eg if input is abcdef
, we have to return fedcba
now, we can divide the problem into a smaller subproblem, by assuming that we can reverse 0-n-2 index, through recursion. So if we can reverse (abcde)f through recursion, we’ll be left with (edcba)f
but we actually need f edcba, we can easily extract the last element of the string, and bring it to the front. And now we return our answer.
The base case is when length of the string is less than or equal to 1, we dont need to reverse it, so we send the string back as it is.
Is the approach clear? Tell me if you have any trouble coding it.
Please note that you can divide it into any kind of subproblem you like, the base case and work done during recursion will change accordingly. But the basic principle involved will stay the same.
please give me that code to understand wisely.
it is not working properly
abhi tak mujhe mera reply nahi mila h???
if i use [ return a[e]+reverse(a,s,e-1);] then it is not working properly
@ankit_verma dry run your code once, you’ll see what is actually happening and why is that logic not working. It is a fairlyy simple code so dry running should not be too complex. (value of e is not changing, so you are actually reversing from the end, so each time you reversed the string from ending, like first you reverse “t” then you reverse “it” and so on, so you need to add the current element, ie a[s] to the end to actually reverse it). Just dry run it once you will see for yourself.
samajh nahi aaya or mene hi wo code likha h isliye run karke dekh liya h.
@ankit_verma i really dont know how to explain like this. I’ll try nonetheless.
i am trying to make the call stacks and follow them along
first call is for a = “ankit”, s = 0, e = 4
next call for a = “ankit”, s = 1, e = 4
next call for a = “ankit”, s = 2, e = 4
next call for a = “ankit”, s = 3, e = 4
next call for a = “ankit”, s = 4, e = 4
now s== e condition satsified, so a.substr(4,4) ie, “t” is returned.
we go to a = “ankit”, s = 3, e = 4
here, we got “t” from the recursive call. Now, if we want to return a reversed string, we have to return “t” + “i”, not “i” + “t”, and to get “i”, we can use s
. We cannot use e
because e is constant throughout the recursion calls and is not iterating through the string, we need a different character each time according to out needs in order to reverse the whole string. You can follow along the rest of the dry run yourself. I hope it is clear now.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.