Monday, 12 September 2011

Read a string and reverse it and then print in alphabetical order.

A string is a contiguous sequence of symbols or values, such as a character string (a sequence of characters) or a binary digit string (a sequence of binary values). In the example given below takes string value from user and reverse that string by using String reverse(String str) method and set in alphabetical order by String alphaOrder(String str) method. Note that passed string by user must be in upper case or in lower case but mixed cases are not permitted for this example.
Program asks the user to enter the input string. Once it is entered by the user, the program performs the reverse operations.
Program: Read a string reverse it and arrange in alphabetical order.
  * read a string and reverse it and then write in alphabetical order.
   import java.util.*;

   class ReverseAlphabetical {

   String reverse(String str) {
   String rStr = new StringBuffer(str).reverse().toString();
   return rStr;
   String alphaOrder(String str){
   char[] charArray = str.toCharArray();
   String aString = new String(charArray);
  return aString ;

   public static void main(String[] args) throws IOException {
  System.out.print("Enter the String : ");
   BufferedReader br =new BufferedReader(new InputStreamReader(;
  String inputString = br.readLine();
  System.out.println("String before reverse : " + inputString);
  ReverseAlphabetical obj = new ReverseAlphabetical();
  String reverseString = obj.reverse(inputString);
  String alphaString = obj.alphaOrder(inputString);
  System.out.println("String after reverse : " + reverseString);
  System.out.println("String in alphabetical order : " + alphaString);
Output of the program:
Enter the String : mahendra
String before reverse : mahendra
String after reverse : ardneham
String in alphabetical order : aadehmnr
Download source code
Post a Comment