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
Output of the program:
Download source code
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); } } |
Enter the String : mahendra String before reverse : mahendra String after reverse : ardneham String in alphabetical order : aadehmnr |
No comments:
Post a Comment