Sunday, July 04, 2010

Sort String

Buzz It
import java.io.DataInputStream;
class sort
{ String s[]=new String[20];
int n;
void read()
{
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the limit");
n=Integer.parseInt(in.readLine());
System.out.println("Enter string to sort");

for(int i=0;i<n;i++)
{
s[i]=(in.readLine());
}
}
catch(Exception e)
{
System.out.println("Exception caught"+e);
}
}
void calc()
{

for(int i=0;i<n;i++)
{
for(int j=i+1;j<n ;j++)
{
if((s[i].compareTo(s[j]))>0)
{
String t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
}
void result()
{System.out.println("the sorted result is");
for(int i=0;i<n;i++)
System.out.println(s[i]);
}
}
class sortstrings
{
public static void main(String args[])
{
sort obj=new sort();
obj.read();
obj.calc();
obj.result();
}
}



OUTPUT:

0 comments:

Post a Comment