What is the control flow of below program? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the control flow of below program?

Public class A { Static string rev Recursively (string str) { return str. length() <2?str:revRecursively(str.substring(1))+str.chatAt(0);} public static void main (String [] args) { String string="edone" ; System.out.print(revRecursively(string)) ;}} Output : node

9th Mar 2019, 9:24 AM
Kayalvizhi.K
Kayalvizhi.K - avatar
2 Answers
+ 4
Process illustrated... !!!I ll take the method name as r for ez reference r("edon") will return r("don") + "e" >>> and the r method will continuously append the first character to the end. So if I wrapped the return value in each call it will look something like this ((("n")+"o")+"d") + "e" As you can see n is the last character returned and then the recursive call will be stopped. After pasting those chars u will get "node" Please refer to the link i provided above as it is too hard to demonstrate this kind of recursion
9th Mar 2019, 9:51 AM
Seniru
Seniru - avatar
+ 3
This is a challenge question right? Well this is a method to reverse a string recursively. https://www.javatpoint.com/recursion-in-java Each time the method get called it will return the last char and it will call the method itself - understanding the recursion is very important to what I m going to say. This process will happen until the length of value passed to the method is less than 2 - 0, 1. And ultimately, you will get a reversed string
9th Mar 2019, 9:44 AM
Seniru
Seniru - avatar