Could anyone please explain why "return str" line never executes?
public static String reverseString(String str){
String reverse="";
if(str.length() == 1){
return str; //at one point this condition will be true, but value never returns
} else {
reverse += str.charAt(str.length()-1) + reverseString(str.substring(0,str.length()-1));
return reverse;
}
}
public static void main(String a[]) {
System.out.println(reverseString("Test"));
}
The line does executes, how can you say it does not executes. I have added the syso statement, it does print, actually you are calling substring
in a recursion, once the length becomes 1, it will execute.
public static String reverseString(String str) {
String reverse = "";
if (str.length() == 1) {
System.out.println("hi");
return str; // at one point this condition will be true, but value never returns
} else {
reverse += str.charAt(str.length() - 1) + reverseString(str.substring(0, str.length() - 1));
return reverse;
}
}
public static void main(String a[]) {
System.out.println(reverseString("Test"));
}
output
hi
tseT
You can easily use StringBuilder#reverse method
public String reverser(String str){
return new StringBuilder(str).reverse().toString();
}
public static String reverseString(String str) {
String reverse = "";
if (str.length() == 1) {
return str; // at one point this condition will be true, but value
// never returns
} else {
String part = reverseString(str.substring(0, str.length() - 1));
System.out.println("Current: " + part); // Print out
reverse = str.charAt(str.length() - 1)
+ part;
return reverse;
}
}
public static void main(String a[]) {
System.out.println(reverseString("Test"));
}
Just add a print within your recursive function, to trace what is happening.
Output:
Current: T // return str
Current: eT // return reverse
Current: seT // return reverse
tseT // final return reverse
From the output, you can convince yourself whether str is being returned.
My Implemention:
public static String reverse(String str) {
if(str.length() > 1) {
return str.substring(str.length()-1)+reverse(str.substring(0, str.length()-1));
} else {
return str;
}
}
public static void main(String[] args) {
System.out.println(reverse("Test"));
}
output:
tseT
Actually it hits and executes, check this out;
public static String reverseString(String str){
String reverse="";
if(str.length() == 1 ){
System.out.println("HIT: " + str); // CHECKING HIT
return str; //at one point this condition will be true, but value never returns
} else {
reverse += str.charAt(str.length()-1) + reverseString(str.substring(0,str.length()-1));
return reverse;
}
}
public static void main(String a[]) {
System.out.println(reverseString("Abcd"));
}
If you run this code, you will see the output as below;
HIT: A
dcbA
To Understand how this code is working, you have to understand how reverse method call itself and completes its process;
Check the image down below;
As you can see, on the 3rd step, because that the recursive function's input string length equals to 1, that part of the code is executed.
Hope that all these helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.