Java Code to Sort Strings in Alphabetical Order - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Monday, September 25, 2017

Java Code to Sort Strings in Alphabetical Order

Write a Java Code to Sort Strings in Alphabetical Order

Program:
import java.util.Scanner;

public class JavaProgram
{
public static void main(String[] input)
{
int i, j;
String temp;
Scanner scan = new Scanner(System.in);

String names[] = new String[5];

System.out.print("Enter 5 Names/Words : ");
for(i=0; i<5; i++)
{
names[i] = scan.nextLine();
}

System.out.println("\nSorting Words/Names in Alphabetical Order...\n");
for(i=0; i<5; i++)
{
for(j=1; j<5; j++)
{
if(names[j-1].compareTo(names[j])>0)
{
temp=names[j-1];
names[j-1]=names[j];
names[j]=temp;
}
}
}

System.out.print("Words/Names Sorted in Alphabetical Order Successfully..!!");

System.out.println("\nNow the List is :\n");
for(i=0;i<5;i++)
{
System.out.println(names[i]);
}
}
}

Output:
Enter 5 Names/Words : Programs for you to learn
be
hAPPY
ALL
TIME

Sorting Words/Names in Alphabetical Order...

Words/Names Sorted in Alphabetical Order Successfully..!!
Now the List is :

ALL
Programs for you to learn
TIME
be
hAPPY

No comments:

Post a Comment