• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need help with Java

VigilanteCS

Senior member
Hi, I'm having trouble with an assignment to reverse a string using recursion ( ex. "green" , "neerg". All I can figure out how to do is have it output the last letter first, nothing else. I'll show you the code from the recursion method down.

public static String reverse(String n)
{
if(n.length() == 1)
{
return n + " ";
}
else
{
//this is where I'm having trouble
return n.substring(n.length() -1, n.length());
}

public static void main(String[] args)
{
recursion recursion = new recursion()'
String x = reverse("green");
JOptionPane.showMessageDialog(null, x);
System.exit(0);
}


When I write reverse in front of n.substring (which I know I'm supposed to do)I get a fatal exception error, without it it outputs "n". I know I'm supposed to have reverse in front of the return statement in the else. I know that the actual code in the else section isn't correct as well. Any help would be greatly appreciated! Thanks!
 
You don't even have a recursive step in there.

The reverse function should look something like this:

 
Well if you want some working c++ code let me know (I have it but couldn't figure out the nice way to post code in these forums). However, what you need to do is have the recursion function take off the last character in the string and return it concatenated onto the result of the next run of the recursion function. Your stopping point is when the length of the string is less than or equal to 1 (in which case you just return it).
 
Originally posted by: jediknight
Here's the basic algo:
REVERSE (String)
if length of string = 1
return string
else
return last character + reverse ( rest of string )


the "reverse rest of string part" is wher I'm having trouble, znaps, I'm going to try that code.

Thanks for the responses
 
I'm a little worried that you don't understand the concept of recursion. Or was it just an error in your initial code? A recursive function is one that calls itself.
 
I understand the concept of recursion when it's on paper. In the computer it definately confuses me.

znaps, that code works great, thank you a lot....just for clarification, can you explain the line that reverses the word? I somewhat understand it, the charAt is confusing me though
 
Well then it seems to me that you just made one of those "d'oh" mistakes because you didn't call reverse on the substring, you just returned it.
 
Originally posted by: torpid
Well then it seems to me that you just made one of those "d'oh" mistakes because you didn't call reverse on the substring, you just returned it.


Well I said that when I did it would give me a fatal exception and crash.

I think this just clicked.
n.substring(1, n.length()) - This will return "reen" then n.charAt(0); will return "g" after "reen"
then it goes through again and n.substring(1, n.length()) will return "een", then n.charAt(0) will return "r" and "g", and it continues and builds "neerg". Am I correct about this?
 
Originally posted by: VigilanteCS
Originally posted by: torpid
Well then it seems to me that you just made one of those "d'oh" mistakes because you didn't call reverse on the substring, you just returned it.


Well I said that when I did it would give me a fatal exception and crash.

It would seem that it is I who have had a d'oh moment.
 
Okay, as has been mentioned, recursion means a function that calls itself. It allows you to write code that most times is truly small compared to an iterative solution.

Now I can think up a way to do this... allow me to 'splain in pseudo/unchecked C++ code:

Code:
string reverse_sub(int position,string reverse_me)
{
   if(getsubStr(reverse_me,position,1) != NULL)
   {
      return(strcat(reverse_sub(position + 1,reverse_me),getsubStr(reverse_me,position,1));
   }
   return "";
}

void reverse(string reverse_me)
{
   cout << reverse_sub(0,reverse_me);
}

Then all you have to do is call the main reverse function. At least in C++. Get it?
 
btw, this would be a whole lot easier for you to figure out if you first figured out how to step through your running code with a debugger. you would be able to tell what is happening at each step of the recursion to see if it's doing what you intend. if you can figure this out on paper first, that's great -- then you have a mental model of what you should see when you're stepping through the code in the debugger.
 
Because my teacher is saying that we have to do it his way using 2 substrings. I think I'm pretty close but I can't figure out why this line wont work

return reverse(n.substring(n.length() -1, n.length())) + n.substring(0, n.length());

EDIT: I got it working

This is the line that works

return (n.substring(n.length() -1, n.length())) + reverse(n.substring(0, n.length()-1));

my problem was in the 1st line i was trying to reverse 1 letter, which is gay.
 
Originally posted by: VigilanteCS
Because my teacher is saying that we have to do it his way using 2 substrings. I think I'm pretty close but I can't figure out why this line wont work

return reverse(n.substring(n.length() -1, n.length())) + n.substring(0, n.length());

Why don't you try:

return n.substring(n.length() -1) + reverse(n.substring(0, n.length()-1));
 
yeah, I just tried that and actually figured it out on my own...so proud of myself =)

Anyway, with the way I had it before I was reversing 1 letter which makes no sense.
 
Back
Top