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



Read a string and reverse it and then print in alphabetical order.
Posted on: June 30, 2008 at 12:00 AM
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).

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.

ReverseAlphabetical.java

  /**
  * read a string and reverse it and then write in alphabetical order.
   */
   import java.io.*;
   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();
   Arrays.sort(charArray);
   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(System.in));
  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

Related Tags for Read a string and reverse it and then print in alphabetical order.:
cstringgitbinarymethodusersedcharordervaluermiusingthissequencesetsymbolscharactercasereversecascharactersforexamplectealphavaluesicalexamssesymbolbieitnotlsmitusepefromceinnormpasscalasmnttrcaalphabetesdigasememixmixedppbinxawerxampsxesuatrackishaivnotemplalphabeticalandaractstrvattseqssriringrdtigthcontiguousbolbelostabalualphabetichatseqicaicapleplmindonomolo


More Tutorials from this section

Ask Questions?    Discuss: Read a string and reverse it and then print in alphabetical order.   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.