About us  |  Contact  |  Sitemap   

< ? php if (is_front_page()) { dynamic_content_gallery(); } ? >

Sort a list of strings LEXICOGRAPHICALLY/ALPHABETICALLY

June 23, 2011

in JAVA,Programming

Sorting a list of strings LEXICOGRAPHICALLY/ALPHABETICALLY means that this program will sort a list of strings in a string array like they appear in the DICTIONARY so that it is easy to search any word by checking it alphabetically as we do it in a pocket dictionary.

This algorithm helps in developing applications like dictionary, etc.
JAVA Programs

import java.util.*;
import java.io.DataInputStream;
class lexic
{
public static void main(String args[])
{
String[] str = new String[10];
String temp;
DataInputStream ds = new DataInputStream(System.in);
try{
System.out.println(“Enter a list of Strings”);
for(int i=0;i<10;i++)
{
str[i] = ds.readLine();
}
}
catch(Exception e)
{
System.out.print(“I/O Error”);
}

for(int i=0;i<10;i++)
{
for(int j=i+1;j<10;j++)
{
if(str[j].compareTo(str[i])<0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
System.out.println(“LEXICOGRAPHICALLY Arranged Strings are”);
for(int i=0;i<10;i++)
{
System.out.println(str[i]);
}

}
}

NOTE : This will take the input of 10 Strings. To take input of more number of Strings, just change the value 10 in the all the  for loops to the number of strings that u want.

Leave a Comment

Previous post:

Next post: