Write a recursive method called reverseString() that takes in a string as a parameter and returns the string in reversed order. The main method is provided to read a string from the user and call the reverseString() method. Use Java. Ex: If the input of the program is: Hello the reverseString() method returns and the program outputs: Reverse of "Hello" is "olleH". Ex: If the input of the program is: Hello, world! the reverseString() method returns and the program outputs: Reverse of "Hello, world!" is "!dlrow ,olleH". Hint: Move the first character to the end of the returning string and pass the remaining sub-string to the next reverseString() method call. import java.util.Scanner; public class LabProgram { /* TODO: Write recursive reverseString() method here. */ public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String input, result; input = scnr.nextLine(); result = reverseString(input); System.out.printf("Reverse of \"%s\" is \"%s\".", input, result); } }
Write a recursive method called reverseString() that takes in a string as a parameter and returns the string in reversed order. The main method is provided to read a string from the user and call the reverseString() method.
Use Java.
Ex: If the input of the
Hello
the reverseString() method returns and the program outputs:
Reverse of "Hello" is "olleH".
Ex: If the input of the program is:
Hello, world!
the reverseString() method returns and the program outputs:
Reverse of "Hello, world!" is "!dlrow ,olleH".
Hint: Move the first character to the end of the returning string and pass the remaining sub-string to the next reverseString() method call.
import java.util.Scanner;
public class LabProgram {
/* TODO: Write recursive reverseString() method here. */
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String input, result;
input = scnr.nextLine();
result = reverseString(input);
System.out.printf("Reverse of \"%s\" is \"%s\".", input, result);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps