Sunday, October 21, 2012

Java program to perform string sorting


import java.io.*;
class stringsort
 { String p[];int i;
 
  stringsort(int q)
  {
    p=new String[q];
  }
 void read() throws IOException
  {BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
   for(int i= 0; i<p.length; i++)

   {System.out.println("Enter the "+(i+1)+"th element");
     p[i]=br.readLine();
   }
  }
 void sort()
  { for (int i=0;i<p.length-1;i++)
    for(int j=i+1;j<p.length;j++)
 
 {
   if (p[i].compareTo(p[j])>0)
  { String temp= p[i];
    p[i]=p[j];
    p[j]= temp;
  }
 }
}
 void disp()
 {System.out.println("The sorted string is:");
  for (int i=0;i<p.length;i++)
  System.out.println(p[i]);
 }
 public static void main(String s[]) throws IOException

 {BufferedReader b= new BufferedReader(new InputStreamReader(System.in));
  System.out.println("How many names?") ;
  int m =  Integer.parseInt(b.readLine());
  stringsort k= new stringsort(m);
  k.read();
  k.sort();
  k.disp();
}
}
Output

0 comments:

Post a Comment