Java Programming: Write a JAVA Program Sorting Strings

Posted on:
tags: ,

     

Write a JAVA Program Sorting Strings
import java.io.*;

public class a14 {


        public static void main(String args[])throws IOException
        {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter the no. of strings");

                String s=br.readLine();
                int n=Integer.parseInt(s);

                String str[]=new String[n];
                System.out.println("Enter the strings");
                for(int i=0;i<n;i++)
                str[i]=br.readLine();

                for(int i=0;i<n-1;i++)
                {
                        for(int j=0;j<n-i-1;j++)
                        {
                                if(((str[j]).compareTo(str[j+1]))>0)
                                {
                                        String t=str[j];
                                        str[j]=str[j+1];
                                        str[j+1]=t;
                                }
                        }
                 }
                  System.out.println("sorted string is");
                for(int i=0;i<n;i++)
                System.out.println( str[i]);
        }
 }
OUTPUT:
Enter the no. of strings
4
Enter the strings
s
d
ff
gg
sorted string is
d
ff
gg
s

No comments:

Post a Comment

< >