Help with writing a void reverse() method and use recursion to reverse a complete sentence(Java)

StackOverflow https://stackoverflow.com/questions/1869434

  •  18-09-2019
  •  | 
  •  

Question

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);  
  }
}
Was it helpful?

Solution

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.

OTHER TIPS

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 :-)

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.

public void reverse()
{
    if(text.length() > 0)
    {
        String first = text.substring(0,1);
        String remaining = text.substring(1);

        Sentence shorter = new Sentence(remaining);
        shorter.reverse();

        text = shorter.text + first;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top