Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

instructions: Write a main method which tests your method by using a sentinel co

ID: 3707227 • Letter: I

Question

instructions:

Write a main method which tests your method by using a sentinel controlled loop which asks the user to enter strings as input.

Be sure to include strings containing white space and special characters as part of your test data.

1. Write a recursive method string reverse (string text ) that reverses a string. For example, reverse("Hello!") returns the string "lolleH". Implement a recursive solution

by removing the first charecter, reversing the remaining text, and combining the two.   

Note :This is a java program. Please follow all steps.

Explanation / Answer

ReverseStringLine.java

import java.util.Scanner;

public class ReverseStringLine {

public static void main(String[] args) {

System.out.println("The following program reads in a sentence from the user and prints it out in reverse.");

Scanner scan = new Scanner(System.in);

char choice = 'y';

do {

System.out.println("Please type a sentence then press enter: ");

String line = scan.nextLine();

System.out.println("Your original sentence: "+line);

System.out.println("The sentence reversed: "+reverse(line));

System.out.println("Would you like to reverse another sentence? (y/n): ");

choice = scan.next().charAt(0);

scan.nextLine();

} while(choice =='y'||choice=='Y');

}

public static String reverse(String s){

String reverseString ="";

for(int i=s.length()-1; i>=0; i--){

reverseString = reverseString+s.charAt(i);

}

return reverseString;

}

}

Output:

The following program reads in a sentence from the user and prints it out in reverse.
Please type a sentence then press enter:
Hello!
Your original sentence: Hello!
The sentence reversed: !olleH
Would you like to reverse another sentence? (y/n):
y
Please type a sentence then press enter:
Suresh
Your original sentence: Suresh
The sentence reversed: hseruS
Would you like to reverse another sentence? (y/n):
n