Results 1 to 2 of 2
- 11-29-2010, 10:43 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
How does this very simple code work?
This is a simple recursion program which reverses the order of characters in a word
my question is waht happens hereJava Code:public class Main { public static String reverse(String str) { if (str.equals("")) { return ""; } else { char first = str.charAt(0); String part = str.substring(1); return reverse(part)+ first ; } } public static void main (String[] args) { System.out.print(reverse("cat")); }
because to me it looks like it should print the word in the same order, the value of "first" is c, then the next time through it becomes a... so why does it print tac? and not cat?Java Code:return reverse(part)+ first ;
- 11-29-2010, 11:43 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Similar Threads
-
Simple file reader won't work in eclipse
By BoomPony in forum New To JavaReplies: 3Last Post: 11-27-2010, 05:16 PM -
Will this code work
By rajat16 in forum XMLReplies: 3Last Post: 09-24-2010, 11:52 AM -
Simple SQL update doesn't work :( (Probebly easy error)
By Addez in forum New To JavaReplies: 4Last Post: 08-23-2010, 03:19 AM -
My Simple Array Does Not Work!
By Simplev_v in forum New To JavaReplies: 16Last Post: 09-07-2009, 02:43 PM -
Simple animation won't work
By nolsen01 in forum New To JavaReplies: 4Last Post: 07-08-2009, 11:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks