06-04-2011, 01:20 PM
Code:
import java.io.*;
class Dem21
{
public static void main(String args[])throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int i,j,n=0;
String s1;
System.out.println("enter the array size:");
try
{
n=Integer.parseInt(b.readLine());
String s[]=new String[n];
System.out.println("enter the strings:");
for(i=0;i<n;i++)
{
s[i]=b.readLine();
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(s[i].compareTo(s[j])>0)
{
String tmp=s[i];
s[i]=s[j];
s[j]=tmp;
}
}
}
System.out.println("sorted names are:");
for(i=0;i<n;i++)
{
System.out.println(s[i]+"\t");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT
E:\3msc\java\meenu>java Dem21
enter the array size:
3
enter the strings:
Rini
Geethu
Arya
sorted names are:
Arya
Geethu
Rini