Write a program in JAVA to input any word and print it in alphabetical order.
ex--input- ARORA
output- AAORR
Answers
public class Alphabetical_Order
{
public static void main(String[] args)
{
int n;
String temp;
System.out.print("Enter number of names you want to enter:");
n = s.nextInt();
String names[] = new String[n];
System.out.println("Enter all the names:");
for(int i = 0; i < n; i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]);
}
}
Enter the number of names: 4
Abhishek
Prashant
Harsh
Ashish
Names in alphabetical order: Abhishek , Ashish , Harsh , Prashant
Answer:
import java.util.*;
class AlphabeticalOrder
{
static void teja()
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter the String : “);
String st = in.nextLine();
int l = st.length();
st=st.toUpperCase();
for(int i = 65;i<=90;i++)
{
for(int j=0;j<l;j++)
{
char ch = st.charAt(j);
if(i==ch)
System.out.print((char)i+” “);
}
}
}
}