Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2. Prompt the user to input a String. 3. Input a String. 4. Use revRec3 to reverse the String input by the user and print

2. Prompt the user to input a String. 3. Input a String. 4. Use revRec3 to reverse the String input by the user and print it out on its own line. 5. Use the given Scanner (see the Getting started code) for all user input. 6. 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

sample output:

Enter a string: Helloworld

dlrowolleH

************************* This program has to have THREE RECURSIVE CALLS heres an example***********************************

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

return rev3("kl") + rev3("ij") + rev3("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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions