write a program in java to accept a string and display the string in alphabetical order
Answers
Answered by
0
import java.util.Scanner;
public class Alpha
{
public static void main(String[] args)
{
int no;
String temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names you want to enter:");
no = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < no;i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < no; i++)
{
for (int j = i + 1; j < no; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Sorted Order:");
for (int i = 0; i < no - 1; i++)
{
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]); }}
}
}
HEY IF THE TExT DISTORTS KINDLY SEE THE ATTCHMENT:
public class Alpha
{
public static void main(String[] args)
{
int no;
String temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names you want to enter:");
no = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < no;i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < no; i++)
{
for (int j = i + 1; j < no; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Sorted Order:");
for (int i = 0; i < no - 1; i++)
{
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]); }}
}
}
HEY IF THE TExT DISTORTS KINDLY SEE THE ATTCHMENT:
Attachments:
Similar questions