Wednesday, 13 March 2013

Java Program For Sorting A Given List Of Names In Alphabetical Order


/* Java Program For Sorting A Given List Of Names In Alphabetical Order */

// File Name: stringsort.java


import java.io.*;
class stringsort {
public static void main(String arg[]) throws IOException {
int i,j,len;
String s[]=new String[5],swp;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter 5 strings ");
for(i=0;i<=4;i++)
s[i]=in.readLine();
for(i=0;i<=3;i++) {
for(j=i+1;j<=4;j++)
if(s[i].compareTo(s[j])>0)
{
swp=s[i];
s[i]=s[j];
s[j]=swp;
}
}
System.out.println("Strings After Sort ");
for(i=0;i<=4;i++)
System.out.println(s[i]);
System.out.println("www.shashanktechnologies.blogspot.com");
}
}


Output:

Compilation: G:/shashank>javac stringsort.java

Interpretation: G:/shashank>java stringsort





No comments:

Post a Comment