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

Develop an application that prompts the user to enter a string (e.g., abcdefg) a

ID: 3688820 • Letter: D

Question

Develop an application that prompts the user to enter a string (e.g., abcdefg) and displays reversed string (e.g., gfedcba). The application must work for any string.
Algorithm: (1) Prompt the user to enter a string. (2) Create an empty 1D array to store the characters in the string (Note: The length of this empty array must match the length of the string) (3) Read each character in the string and store it in the array. (4) Display the reversed string by displaying the last element to the first element of the array

Explanation / Answer

Hello there ,

Please find below code and it's O/P.

I have designed it with the given algorithm in the question .

Let me know if you have any queries.

Thanks.

import java.util.Scanner;

/**
* This class is a Driver program that reverses the string given by user.
*
* @author dipal.prajapati
*
*/
public class StringReverse {

   public static void main(String args[]) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("Enter String to be reversed: ");
       String stringObj = scanner.next();
       reverseString(stringObj);
   }

   /**
   * It reverses the string bases on the algorithm provided in the question.
   *
   * @param stringObj
   */
   public static void reverseString(String stringObj) {
       char[] charArray = stringObj.toCharArray();
       System.out.print("Reverse String for "" + stringObj + "" is : ");
       for (int i = charArray.length - 1; i >= 0; i--) {
           System.out.print(charArray[i]);
       }
   }
}

====O/P====

Enter String to be reversed:
abcdefg
Reverse String for "abcdefg" is : gfedcba