Q:

(Reverse a string) Write a program that prompts the user to enter a string and displays the string in reverse order

0

(Reverse a string) Write a program that prompts the user to enter a string and displays the string in reverse order.

Output:

Enter a string: ABCD
The reversed string is DCBA

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*
(Reverse a string) Write a program that prompts the user to enter a string and
displays the string in reverse order.
*/
import java.util.Scanner;

public class Exercise_05_46 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		// Prompt the user to enter a string
		System.out.print("Enter a string: ");
		String string = input.nextLine();

		// Reverse the string
		String reverse = "";
		for (int i = string.length() - 1; i >= 0; i--) {
			reverse += string.charAt(i);
		}

		// Display the string in reverse order
		System.out.println("The reversed string is " + reverse);
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now