Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. /* WAP to declare a String array of size n, sort and display the array alphabetically*/
  2. import java.util.*;
  3. public class Program
  4. {
  5.     public static void main(String[] args) {
  6.         Scanner s = new Scanner (System.in);
  7.         System.out.println("Enter the length of the array");
  8.         int n = s.nextInt();
  9.         String arr[] = new String [n];
  10.         String temp= "";
  11.         int c;
  12.         System.out.println(arr.length);
  13.         System.out.println("Enter the Strings");
  14.         for(int i = 0; i<arr.length; i++)
  15.         {
  16.             arr[i] = s.nextLine();
  17.             System.out.println(arr[i]);
  18.         }
  19.         for (int i = 0; i<arr.length; i++)
  20.         {
  21.             for (int j = 0; j<arr.length -1; j++)
  22.             {
  23.                 c = arr[j].compareTo(arr[j+1]);
  24.                 if (c>0)
  25.                 {
  26.                     temp = arr[j];
  27.                     arr[j] = arr[j+1];
  28.                     arr[j+1] = temp;
  29.                 }
  30.             }
  31.         }
  32.         for (int i = 0; i<arr.length; i++)
  33.         {
  34.             System.out.println(arr[i]);
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement