Question: This exercises uses the String method toCharArray and a loop to display the letters in the string in reverse order. Write a simple program that

This exercises uses the String method toCharArray and a loop to display the letters in the string in reverse order.

Write a simple program that accepts a phrase as input and reverses the order of the letters and spaces in the phrase. Example Input The rain in Spain falls mainly on the Plain Example Output nialP eht no ylniam sllaf niapS ni niar ehT This may be a great place to use a for loop that decrements instead of increments. Like "for(int d = string.length() - 1; d >= 0; d--)"

What I have so far (What is wrong with my code) :

import java.util.*; /** * Date Created: * @author Student name * Filename: ReverseOrder.java */ public class ReverseOrder {

/** * This is the main method that will test the ReverseOrder class and display * the results to the user * @param args */ public static void main(String[] args) {

//******** Write your code here ********** //allow the user to input their own string phrase String input = "";

//ask the user to enter their string of characters System.out.println("Please enter the phrase that you want to be reversed");

//The use of try will catch exceptions that program might experience at execution try { //read the user's input and place it in a character array named answer BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); input = br.readLine(); char[] answer = input.toCharArray();

//for loop will scan the string from end to start for (int i = answer.length - 1; i >= 0; i--) //print the characters in the array one by one { System.out.print(answer[i]); } } // catch will handle the exceptions that might occur catch (IOException e) {

//error-handling code to handle any exceptions e.printStackTrace(); }

} // end main } // end ReverseOrder class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!