Java program to accept strings and arrange it in lexicographically order

Java program to accept strings and arrange it inn lexicographically order(dictionary order)


import java.util.Scanner;

public class demo{

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the String: ");

String str = sc.nextLine();

sc.close();

String strArray[] = str.split(" ");

String temp="";

for(int i=0;i<strArray.length-1;i++) {

    for(int j=0;j<strArray.length-1-i;j++) {

        if(strArray[j].compareToIgnoreCase(strArray[j+1])>0){

    temp = strArray[j];

        strArray[j] = strArray[j+1];

strArray[j+1] = temp;

}

}

}

str = "";

for(String x : strArray)

    str = str + " " + x;

System.out.print(str);

}

}

Comments