Results 1 to 4 of 4
- 12-29-2008, 05:27 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 7
- Rep Power
- 0
Understanding this recursive function
Hi guys, I have been having a problem understanding this particular piece of code, which is a recursive function that prints a string backward..
Java Code:public static void writeBackward(String s) { if (s.length() > 0) { // write the rest of the string backward writeBackward(s.substring(1)); // [COLOR="DarkRed"]need help understanding what is happening here, how does this technique work in writing the string backward[/COLOR] // print the first character System.out.print(s.charAt(0)); //[COLOR="DarkRed"]I dont understand why it is printing the string from the first position (charAt(0), according to the statement), when it should be printing from the last position to the first. Unless I am somehow grossly missing out on something here.[/COLOR] } System.out.println(); }
Appreciate your help in interpreting this, thanks :)
- 12-29-2008, 05:49 PM #2
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
Sometimes it helps me when I come across recursion just to write all the functions inline with a sample parameter. (And then change it back, of course, so it works again.)
Hope that helps.PHP Code:public static void writeBackward(String s)//asd { if (s.length() > 0) { writeBackward(s.substring(1))//sd { if (s.length() > 0) { writeBackward(s.substring(1))//d { if (s.length() > 0) { writeBackward(s.substring(1))// { if (s.length() > 0) { writeBackward(s.substring(1));//Doesn't get called. System.out.print(s.charAt(0)); } } System.out.print(s.charAt(0));//d } } System.out.print(s.charAt(0));//s } } System.out.print(s.charAt(0));//a } }
- 12-29-2008, 08:28 PM #3
Trying to figure out a recursive method can be a real mind–twister.
One way to explore this is to try to follow/print enough of the execution of each trip thru the method to get an idea of what's going on.
Java Code:public class Test { public static void main(String[] args) { writeBackward("hello world", 0); } public static void writeBackward(String s, int count) { if (s.length() > 0) { // write the rest of the string backward System.out.println("recurse with \"" + s.substring(1) + "\"" + pad(count) + " will print \"" + s.charAt(0) + "\" later"); writeBackward(s.substring(1), count+1); // Since the line above called this method this next line // will have to wait until the chain/cascade of recursive // calls ends before it can execute. The order of execution // of these next lines will be in reverse order (inside-out) // relative to the initial chain of (recursive) calls. // print the first character System.out.printf("printing charAt(%2d) ", count); System.out.print(s.charAt(0)); } System.out.println(); } private static String pad(int n) { String s = ""; for(int i = 0; i < n; i++) { s += " "; } return s; } }
- 12-30-2008, 05:26 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 7
- Rep Power
- 0
Thanks guys for illustrating the process of how the recursive function works in this instance.
What I understood was how the string length was being reduced with each call of the method, till the length was 0.
What I did not understand was how the println(s.charAt(0)) statement caused the string to be printed backwards.
My guess is that I was not getting the 'unwinding' part of the recursive function, where after the method has been called till the length of the string is 0, the unwinding portion of the recursion causes the string to be printed backwards, as shown by both pieces of code by hardwired and Durnus (thanks!)
However, intuitively, I havent understood how the unwinding part works though...any advise? ?
Similar Threads
-
[SOLVED] Trouble understanding or expressions
By hungdukie in forum New To JavaReplies: 1Last Post: 11-23-2008, 01:24 AM -
[SOLVED] Understanding if else statements
By hungdukie in forum New To JavaReplies: 6Last Post: 11-18-2008, 07:56 AM -
[SOLVED] Help with understanding nullpointexcepion
By soxfan714 in forum New To JavaReplies: 4Last Post: 11-11-2008, 04:51 AM -
Understanding Vectors
By cbrown08 in forum New To JavaReplies: 7Last Post: 11-05-2007, 06:56 PM -
Help with recursive function in java
By cachi in forum Advanced JavaReplies: 2Last Post: 07-31-2007, 06:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks