The following program illustrates the sorting of letters in a string.In this sort() method is used.It will sort all the letters in a string in ascending order.
import java.util.*;
import java.io.*;
import java.lang.String;
class sortletters
{
public static void main(String args[]) throws Exception
{
System.out.println("enter a string");
BufferedReader k = new BufferedReader(new InputStreamReader(System.in));
String s=k.readLine();
char[] data=s.toCharArray();
java.util.Arrays.sort(data);
String s1=new String(data);
System.out.println("After Sorting");
System.out.println(data);
}
}
Output:
enter a string
example
After sorting
aeelmpx