Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) { String s1;
Scanner sc = new Scanner(System.in); System.out.println("Enter string: "); s1 = sc.nextLine(); int i, j, l = s1.length(); char ch[] = new char[l]; char temp; for (i = 0; i < s1.length(); i++) { ch[i] = s1.charAt(i); } for (i = 0; i < l; i++) { for (j = i + 1; j < l; j++) { if (ch[i] > ch[j]) { temp = ch[i]; ch[i] = ch[j]; ch[j] = temp; } } } String s2 = ""; System.out.println("Sorted strings:"); for (i = 0; i < s1.length(); i++) { s2 = s2 + ch[i]; } System.out.println(s2); }}
Output:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String s1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter string: ");
s1 = sc.nextLine();
int i, j, l = s1.length();
char ch[] = new char[l];
char temp;
for (i = 0; i < s1.length(); i++) {
ch[i] = s1.charAt(i);
}
for (i = 0; i < l; i++) {
for (j = i + 1; j < l; j++) {
if (ch[i] > ch[j]) {
temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
}
}
}
String s2 = "";
System.out.println("Sorted strings:");
for (i = 0; i < s1.length(); i++) {
s2 = s2 + ch[i];
}
System.out.println(s2);
}
}
Tags:
Java Programs