Why isnt this returning the new string?
Posted
by
Evan Kimia
on Stack Overflow
See other posts from Stack Overflow
or by Evan Kimia
Published on 2011-02-05T23:02:55Z
Indexed on
2011/02/05
23:25 UTC
Read the original article
Hit count: 441
I have a recursive method that reversed a string (HW assignment, has to be recursive). I did it....but its only returning the value of the string after the first pass. By analyzing the output after each pass i can see it does do its job correctly. heres my code, and the output i get below it:
String s = "Hello, I love you wont you tell me your name?";
int k=0;
public String reverseThisString(String s) {
if(k!=s.length()) {
String first =s.substring(0,k)+s.charAt(s.length()-1);
String end = ""+s.substring(k, s.length()-1);
k++;
s=first+end;
System.out.println(s);
this.reverseThisString(s);
}
return s;
}
output:
?Hello, I love you wont you tell me your name
© Stack Overflow or respective owner