views:

580

answers:

3

I already wrote something that removes the first character of a string and puts it after the remaining substring and then prints it out, the instructions were to reverse the sentence using recursion by removing the first letter of the sentence and concatenate it to the reversed remaining substring, ie. "Hello" yields "olleH". But i dont know about the recursion part, any help would be appreciated, thanks. This is my code:

public class Sentence {

   private String sentence;

   public Sentence(String astring) {
      sentence = astring;
   }

   public void reverse(){

   String firstChar = sentence.substring(0,1);

   String remainingSen = sentence.substring(1,sentence.length());

   System.out.println(remainingSen+firstChar);  
  }
}
+1  A: 

Seeing that this is a homework assignment, I'll give some hints to get started:

  • a recursive method calls itself to do part of the work
  • a reverse() method taking a String argument and returning the reversed version of the string could call itself.
  • if you remove the first character and add it to the end of the reversed left over, your work is done.

If you work out the hints above, you should have solved your problem :-)

rsp
A: 

Do you understand the concept of recursion? If so, figure out the base case (the condition that would stop the recursion) and what you want to do in each recursive step. Hint: you'll need a recursive method that takes the string to be reversed and returns the reversed string. Not going to straight up answer a HW question, but that should get you started.

EDIT: (One more hint) don't try to make the void reverse() method recursive. Have it call a different, private recursive method that actually does the reversing.

Donnie DeBoer
+1  A: 

Generally, if you want to write a recursive function, you will be calling the function within itself. For example:

void fn() {
    fn() 
}

This example will obviously be an infinite loop.

In your case, you want to call your reverse function repeatedly until you reach a defined state (where Hello is transformed to olleH).

public class Sentence {

  private String sentence;

  // ... etc ...

  public void reverse() {
      // Base Case: When do you want this to end? This statement is designed
      // to end the recursion when a desired state is reached

      // some sort of string manipulation (which you have already worked on)

      // call reverse() to continue the 'looping' until 
      // a desired _case_ is reached
  }
}

I assume this is a homework question and it's due soon, so I'm not going to provide an exact answer...

Update 1 : I modified the reverse example to match the constraints that were expressed.

rynmrtn
yes...i know that, but i cant call the reverse method inside of itself since it says its not defined for the type String :/... and I would use another method like public String reverse(String args), but the instructor does not want this, she wants a void reverse() method ...
alfie90
This means you are developing around a design constraint - a classic CS problem. My original example still stands, except that you would call reverse() instead of reverse(modifiedString). Instead of passing an updated string (modifiedString) back into your method, your professor wants you to change the instance variable defined as `sentence`
rynmrtn
Then create 2 methods. A `public void reverse()` and a `private String reverse(String s)`. The `void` one obviously executes the non-void other.
BalusC
the case can be if(sentence.length()==1){ System.out.println(sentence);} ?
alfie90
Couple of questions for you with regard to your response:1. Where are you storing the string as it is reversed?2. Is the length of sentence decreasing?
rynmrtn
what statement will exactly reverse the sentence for me, and 2.no, i should do that right?...
alfie90
Let's go through a simple example with the `sentence = "abc"`. Assume reverse() is called. Here is the necessary sequence of events:1. Check to see if the string is reversed (the base case). If so, we're done.2. Modify a character within `sentence` (in your code, you modified the string "abc" to be "bca")3. Call reverse() to continue the process4. Again, use your base case: Check to see if the string is reversed. If so, stop.5. Modify a character within `sentence` (in your code, your string would change from "bca" to "cab" - oops, your string reversal algorithm isn't quite right!)...
rynmrtn
but how do i compare if the sentence was already reversed, and what really bothers me is that i cant call the reverse method more than once because its not valid for strings, and im using "Sentences", i have never had so much problems with a java code before, I am sorry i keep bothering you with this.
alfie90
If you look at the scope of `sentences`, the variable is available to the method `reverse()`. Therefore, you don't _have_ to have it as a parameter. As `reverse()` recurses, you can continuously rewrite the value of `sentences` until the string/sentence is reversed.
rynmrtn
i got something and it worked, but i still have doubts in recursion, guess ill study more about it during christmas vacations, but thanks a million, i got it working , take care...happy coding
alfie90
good to hear and glad to help. stackoverflow is the place to come if you have questions. I would continue to practice recursion as it's a fundamental practice in computer science (especially when dealing with tree data structures)
rynmrtn