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

 
 

Share on Google+Share on Google+

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

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 30, 2008

Related Tutorials

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
 
 
Comments:4
sushilkumar
April 19, 2011
java

write a java program that display the number of characters, lines and words of entered sentances.
Olivia
September 24, 2011
hlxxlESxMUlvhpFVy

More posts of this quality. Not the usual c***, pselae
pk maur
November 1, 2011
Java

thanks........
Md nasim
April 20, 2012
Java

Thanks
DMCA.com