Write a Java program to get 'N' words from the user and arrange the 'N' words in alphabetical order.
Input:
5
cab
acb
abc
bac
bca
Input:
5
cab
acb
abc
bac
bca
Output:
abc
acb
bac
bca
cab
Java Implementation:
import java.util.*; import java.lang.*; import java.io.*; class CodeRegister { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int numberOfWords = in.nextInt(); String str[] = new String[numberOfWords]; for(int i = 0; i<numberOfWords; i++) { str[i] = in.next(); } sortWords(str); //print the sorted words for(int i = 0; i<numberOfWords; i++) { System.out.println(str[i]); } } static void sortWords(String str[]) { for(int i = 0;i<str.length-1; i++) { for(int j = i+1; j<str.length; j++) { int value = str[i].compareTo(str[j]); if(value > 0) { String temp = str[i]; str[i] = str[j]; str[j] = temp; } } } } }