You are on page 1of 2

Q10.

Write a java program to input a String and print all the characters of the string in

alphabetical order

INPUT :- ALARM

OUTPUT :- AALMR

Ans:

/**

* Write a program in Java to accept a string. Arrange all the letters of the string in an alphabetical order.

* @author (your name)

* @version (a version number or a date)

*/

import java.util.Scanner;

public class StringSort

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("Enter a string:");

String str = in.nextLine();

str = str.toLowerCase();

int len = str.length();

String sortedStr = ""; //Empty String

for (char ch = 'a'; ch <= 'z'; ch++) {

for (int i = 0; i < len; i++) {

char strCh = str.charAt(i);

if (ch == strCh) {

sortedStr += strCh;

}
System.out.println("Alphabetical order:");

System.out.println(sortedStr);

Output:

Enter a string:

Rounak

Alphabetical order:

Aknoru

Variable name Data type Purpose

You might also like