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; }
|
Output of the program:
Enter the String : mahendra String before reverse : mahendra String after reverse : ardneham String in alphabetical order : aadehmnr |