GPicasso
asked on
Recursively reversing a string in java
I have a program to reverse a string recursively in java.. I suspect that it doesn't work right due to arrays getting passed by reference. Could someone point out or correct my mistake?
mport java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class ReverseString {
public static String reverse(String s)
{
//
if (s.length() <= 1)
{
return s;//if last letter in string return that letter
}
else //else
{
char[] arr = new char[s.length()-2];
s.getChars(1, s.length()-1, arr, 0);
char tail = s.charAt(s.length()-1); //tail = string[tail]
char head = s.charAt(0); //head = string[head]
System.out.println(new String(arr) + " " + tail + head);
return tail + reverse(new String(arr) + head);
}
//else
//tail = string[tail]
//head = string[head]
//string.remove(tail)
//string.remove(head)
//return tail + reverse(string) + heqad
}
}
You just have one parenthesis in wrong place
ASKER
Unfortunately I have to do this recursively as part of a homework assignment. This is normally the time when I would ask my professor for some guidance I have a really bad professor for this class, so you guys will have to fill in.
Code:
public class ReverseString {
public static String reverse(String s)
{
//
if (s.length() <= 1)
{
return s;//if last letter in string return that letter
}
else //else
{
char[] arr = new char[s.length()-2];
s.getChars(1, s.length()-1, arr, 0);
char tail = s.charAt(s.length()-1); //tail = string[tail]
char head = s.charAt(0); //head = string[head]
System.out.println(new String(arr) + " " + tail + head);
return tail + reverse(new String(arr)) + head;
}
//else
//tail = string[tail]
//head = string[head]
//string.remove(tail)
//string.remove(head)
//return tail + reverse(string) + heqad
}
public static void main(String [] args){
String s = "meat dog team";
System.out.println(s);
System.out.println(reverse(s));
}
}
Output:meat dog team
eat dog tea mm
at dog te ae
t dog t ea
dog tt
dog
o gd
maet god taem
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
A paren in the wrong place? Hmmm not seeing it could you provide a line number or hint?
I should note this code compiles and doesn't cause any red lines in my IDE.. also the println was simply for debugging purposes. here is what's getting passed around in recursion
Input: AbCdEfGhIjKl
debugging format
[Trimmed String] [tail][head]
I should note this code compiles and doesn't cause any red lines in my IDE.. also the println was simply for debugging purposes. here is what's getting passed around in recursion
Input: AbCdEfGhIjKl
debugging format
[Trimmed String] [tail][head]
run:
bCdEfGhIjK lA
CdEfGhIjK Ab
dEfGhIjK bC
EfGhIjK Cd
fGhIjK dE
GhIjK Ef
hIjK fG
IjK Gh
jK hI
K Ij
jK
BUILD SUCCESSFUL (total time: 1 second)
Finally Returning: lAbCdEfGhIjK
ASKER
Good eye!!!
I already posted it (line 28 of your original code):
return tail + reverse(new String(arr) + head);
should be
return tail + reverse(new String(arr) )+ head;
return tail + reverse(new String(arr) + head);
should be
return tail + reverse(new String(arr) )+ head;
You are always welcome.
Please look at this and let me know if you want more help with it:
(taken from
http://javahowto.blogspot.com/2006/09/reverse-string-with-stringbuilder-or.html)
public static void main(String[] args) {
StringBuilder old = new StringBuilder(args[0]);
StringBuilder newsb = old.reverse();
System.out.println("origin
System.out.println("new string: " + newsb);
}