View the step-by-step solution to:

Question

Q1.jpg

I am confused about how to produce a java for

reverserecursion, can any one give me an example?

Q1.jpg

Task #1 Develop a left recursive method to reverse a string
Develop a method with the prototype
public static String reverseRecursiveLeft (String input)
based on selecting the leftmost character as the head and the remaining part of the string as
its tail. Here is the recursive design.
1. Base case: The problem is trivial when the string length is 0 or 1.
2. Decomposition: For strings of length > 1:
Extract its head (character) and the tail. You are expected to know the
string methods needed to achieve this.
Make a recursive call to obtain the tail reversed.
3. Composition: Append (concatenate) the head to obtain the original string reversed.
Task #2 Develop a right recursive method to reverse a string
Develop a method with the prototype
public static String reverseRecursiveRight (String input)
based on selecting the rightmost character as the head and the remaining part of the string
on its left as the tail. The only difference in the recursive design is that the reversed tail
needs to be appended after the head character.
Page 1 of 2
Task #3 Develop a middle recursive method to reverse a string
Develop a method with the prototype
public static String reverseRecursiveMiddle (String input)
This time, instead of extracting one element (character) from left or right, extract two
characters simultaneously from both left and right. design. This leads to a drop of 2 in size
of the reduced problem. Be careful to take this into account when you design the base
case. Should the base case be any different? Consider odd and even size strings

Top Answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question