Reverse String in Java
Posted by Samath
Last Updated: July 30, 2013
  2797

Read a single sentence and print the sentence in a reverse order. The sentence will be a maximum of one hundred (100) characters long and a minimum of ten (10) characters long. 

Sample Output: 

 Enter a String: I am Legend! 

 Output: !dnegeL ma I

 

Screen Shot:

http://i1271.photobucket.com/albums/jj626/samath89/javainput_zps3f1c5501.png

 

http://i1271.photobucket.com/albums/jj626/samath89/Javaoutput_zps78c4f44d.png

 

Code:

import javax.swing.JOptionPane;

public class reverse

{

      public static void main(String[] args)

      {

            String input_message;

            String input_new = "";

            for(;;)

            {

               input_message = JOptionPane.showInputDialog("Enter a String (Max 100, Min 10)");

               if(input_message.length() >= 10 && input_message.length() <= 100)

            {                  

                           input_new = new StringBuffer(input_message).reverse().toString();

               JOptionPane.showMessageDialog(null, input_new + " ", "Output:",JOptionPane.PLAIN_MESSAGE);

               break;

            }

              else

            {

                  continue;

            }

            }

      }

}