2. Prompt the user to input a String. 3. Input a String. 4. Use revRec3 to rever
ID: 3753152 • Letter: 2
Question
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 );
}
}
Explanation / Answer
package com.chegg.reverse.test; /*package name of the below class.*/
// imported the Scanner package to use Scanner class in the program
import java.util.Scanner;
/**
* The below class is used to reverse a string using recursive method
*
*/
public class StringReverse {
/* method to print reverse of the passed string */
public String reverseString(String str){
if (str.length() == 0) {
return str;
}
/* */
return reverseString(str.substring(1)) + str.charAt(0);/* Recursive call and return the result to calling function */
}
/* Driver method*/
public static void main(String[] args)
{
StringReverse obj = new StringReverse();
System.out.println("Enter string to reversed: ");
//Read the input from the user
Scanner scanner = new Scanner(System.in);
String userString= scanner.nextLine();
//Use reverseString() method to reverse the String
String reveserdString = obj.reverseString(userString);
System.out.println("Original String : "+userString);
System.out.println("Reversed String : "+reveserdString);
}
}
/* Explanation: Recursive function (reverseString) takes string pointer (str) as input and calls itself with next location to passed pointer (str+1).
* Recursion method reverseString continues this way until pointer reaches ‘’, all calls accumulated in stack with character at passed location (str) and return one by one.*/
/* Output of the program will be as below :
Enter string to reversed:
Hello
Original String : Hello
Reversed String : olleH
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.