Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* WAP to declare a String array of size n, sort and display the array alphabetically*/
- import java.util.*;
- public class Program
- {
- public static void main(String[] args) {
- Scanner s = new Scanner (System.in);
- System.out.println("Enter the length of the array");
- int n = s.nextInt();
- String arr[] = new String [n];
- String temp= "";
- int c;
- System.out.println(arr.length);
- System.out.println("Enter the Strings");
- for(int i = 0; i<arr.length; i++)
- {
- arr[i] = s.nextLine();
- System.out.println(arr[i]);
- }
- for (int i = 0; i<arr.length; i++)
- {
- for (int j = 0; j<arr.length -1; j++)
- {
- c = arr[j].compareTo(arr[j+1]);
- if (c>0)
- {
- temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- }
- for (int i = 0; i<arr.length; i++)
- {
- System.out.println(arr[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement