View Single Post
  #3 (permalink)  
Old 07-31-2007, 08:51 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Code:
public class RecursionTest { public static void main(String[] args) { String s = "Hello World"; int len = s.length(); System.out.println("reversal("+s+") = " + reversal(s)); System.out.println("move("+s + ", "+len+") = " + move(s, len)); } public static String reversal(String x){ int y = x.length(); String s =""; for(int j = y-1; j >= 0; j--) s+=x.charAt(j); return s; } private static String move(String s, int n) { if(n < 1) return s; char c = s.charAt(0); s = s.substring(1, n) + String.valueOf(c) + s.substring(n); //System.out.printf("s(1, %d) = %s c = %s s(%d) = %s%n", // n, s.substring(1, n), // String.valueOf(c), // n, s.substring(n)); return move(s, n-1); } }
Reply With Quote