Reputation: 9
I am new to programming and am unable to solve this problem. I have tried changing the static, calling the method different ways, and am still unable to make it call back to itself. The point of the program is to let the user input a string and then output the reverse of the string.
public class StringReverse
{
public static String reverse(String s)
{
int i = s.length()-1;
String letter = "";
if(i == 0)
{
return""; //Base Case for the recusive method.
}
else
{
return letter = s.substring(s.length() - 1) + reverse(s.substring(s.length() - 1));
}
}
public static void main(String[] args)
{
String input = "";
Scanner in = new Scanner(System.in);
do{
System.out.println("\nEnter q to quit.");
System.out.println("Enter a word...(No UpperCase letters)");
input = in.nextLine();
String reversedWord = reverse(input);
System.out.print(reversedWord);
}while(!input.equals("q"));
}
}
Upvotes: 0
Views: 680
Reputation: 48600
No need to create unnecessary variables. Plain and simple.
public static String reverse(String s) {
if (s == null || s.length() < 1)
return "";
return s.substring(s.length() - 1) + reverse(s.substring(0, s.length() - 1));
}
Upvotes: 0
Reputation: 505
The recursion is working, but you are passing the wrong input in. This is what you want:
public static String reverse(String s)
{
int i = s.length()-1;
if(i == -1)
return "";
return s.substring(i) + reverse(s.substring(0, i));
}
There are two reasons yours isn't working. One, by testing for i == 0 instead of i == -1 for the base case, you would always cut off the last letter of the backward string. Two, when you call the recursive method you are passing in just the last character of the string instead of everything except the last character of the string.
Upvotes: 0
Reputation: 37
import java.util.Scanner;
public class JavaApplication3 {
public static String reverse(String s)
{
int i = s.length()-1;
String letter = "";
if(i == -1)
{
return"";
}
else
{
return letter = s.substring(s.length() - 1) + reverse(s.substring(0, s.length() - 1));
}
}
public static void main(String[] args)
{
String input = "";
Scanner in = new Scanner(System.in);
do{
System.out.println("\nEnter q to quit.");
System.out.println("Enter a word...(No UpperCase letters)");
input = in.nextLine();
String reversedWord = reverse(input);
System.out.print(reversedWord);
}while(!input.equals("q"));
}
}
That was the answer. Here is what you did wrong:
First, you were only returning the last character. s.substring(int x) goes from the character at position x to the end of the string. Secondly, since you do -1 each time, your base case should be at -1
Upvotes: 0
Reputation: 32831
I think you want to do that:
return letter = s.substring(s.length() - 1) + reverse(s.substring(0, s.length() - 1));
UPDATE:
Oh, and also:
int i = s.length();
Upvotes: 1
Reputation: 1260
The problem is in the following line:
return letter = s.substring(s.length() - 1) + reverse(s.substring(s.length() - 1));
It appears you're returning the last letter and the value of calling reverse on the last letter. I think you really mean to do something like
return letter = s.substring(s.length() - 1) + reverse(s.substring(0, s.length() - 1));
If this was called with the String "ABC" the first iteration would give something like:
"C" + reverse("AB")
Upvotes: 0
Reputation: 771
Here's your revers function:
public static String reverse(String s)
{
if(s.length() <= 1)
{
return s;
}
else
{
return s.substring(s.length()-1) + reverse(s.substring(0,s.length()-1));
}
}
Make sure to check for null as well.
Upvotes: 1
Reputation: 200138
When you call
s.substring(s.length() - 1)
you have called it with just the last character. The effect cannot be string reversal.
Upvotes: 0