Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

recursively reverse a string using three recursive calls Java. 1. The revRec3 method must be implemented recursively in which you make at least THREE recursive

recursively reverse a string using three recursive calls Java.

1. The revRec3 method must be implemented recursively in which you make at least THREE recursive calls. Each recursive call should be passed a String whose length is roughly str.length() / 3. VERY IMPORTANT

Here is an example of how the revRec3 should be implemented

revRec3("hijkl") // the string inserted to the method

if (str.isEmpty()) // base case return str;

return revRec3("kl") + revRec3("ij") + revRec3("h") = "lkjih" // the return method with three recursive calls

here is my code so far:

import java.util.Scanner; public class recursion {

//driver method public static void main(String[] args) { String str; System.out.println("Enter String: "); Scanner sc = new Scanner(System.in); str = sc.nextLine(); String res = revRec3(str); System.out.println("The reversed string is: " + res);

}

public static String revRec3(String str) { if (str.isEmpty()) return str;

String first = str.substring(0,str.length() / 3 ); String second = str.substring(str.length() / 3, ((2 * str.length()) / 3)); String third = str.substring((2 * str.length()) / 3, str.length()); System.out.println("First: " + first); System.out.println("Second: " + second); System.out.println("Third: " + third); char last = first.charAt(first.length() - 1 ); // char last2 = second.charAt(second.length() - 1 ); // char last3 = third.charAt(third.length() - 1);

return last + revRec3(first.substring(0, (first.length() - 1 ))); // + last2 + str.substring(str.length() / 3, ((2 * str.length()) / 3) - 1 ) // + last3 + str.substring((2 * str.length()) / 3, str.length() - 1 );

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions