Java Program to Sort string in alphabetical order



Program complied with Netbeans IDE you can also use JDK and compile in command prompt .


PROGRAM:


package alphaorder;
import java.io.*;
import java.util.*;
public class alphaorder {
public static void main(String[] args)throws IOException {
BufferedReader st=new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter the string ");
str=st.readLine();
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
String nstr = new String(charArray);
System.out.println("The sorted string is "+nstr);

}
}


OUTPUT:


Enter the string
dcba
The sorted string is abcd

1 comments:

Unknown said...

package practice;

public class OrderingTexts {

public static void main(String[] args){
String text = "gfedcba";

char[] inte = new char[text.length()];

char[] temp = new char[text.length()];

for(int i = 0; i < text.length(); i++){
inte[i] = text.charAt(i);
temp[i] = text.charAt(i);
}

for(int j = 0; j < text.length(); j++){
for(int i = 0; i < text.length() - 1; i++){
if(inte[i] > inte[i + 1]){
inte[i] = temp[i + 1];
inte[i + 1] = temp[i];
temp[i] = inte[i];
temp[i + 1] = inte[i + 1];
}
}
}

System.out.println(inte);

}
}

Post a Comment

If You Are Asking Some Question On This Comment Then Click On Subscribe by email Link