Java String.CASE_INSENSITIVE_ORDER used with Collections.sort()

Description

Java String.CASE_INSENSITIVE_ORDER used with Collections.sort()

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
  public static void main(String[] argv) throws Exception {
    String[] strArray = new String[] { "CSS", "css", "CSS", 
        "HTML","demo2s.com","Demo2s.com" };
    List<String> list = Arrays.asList(strArray);
    System.out.println(list);//from   ww w .jav  a  2 s.c  o  m
    
    Collections.sort(list);
    System.out.println(list);

    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
    System.out.println(list);

    Collections.sort(list, Collections.reverseOrder());
    System.out.println(list);

    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
    System.out.println(list);
    
  }
}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    String[] strings = {"CSS", "css","java","HTML", "Java", "Javascript"};

    System.out.printf("Original strings: %s%n", Arrays.asList(strings));

    // strings in upper case
    List<String> list = 
          Arrays.stream(strings)//from  w w w  . j a va  2  s.  c om
                .sorted(String.CASE_INSENSITIVE_ORDER)
                .collect(Collectors.toList());


    System.out.printf("strings:" + list);
 }
}

Question

We would like to write a program that prompts the user to enter two strings

Display the largest common prefix of the two strings.

Here are some sample runs:

Enter the first string: Welcome to C++ 

Enter the second string: Welcome to programming 
The common prefix is Welcome to 

Enter the first string: food
Enter the second string: care

food and care have no common prefix 
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter first string: ");
    String s1 = input.nextLine();
    System.out.print("Enter second string: ");
    String s2 = input.nextLine();

    //your code here

  }//ww w .ja  v a  2s.c  o  m



}



import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter first string: ");
    String s1 = input.nextLine();
    System.out.print("Enter second string: ");
    String s2 = input.nextLine();

    System.out.println(commonPrefix(s1, s2));

  }

  public static String commonPrefix(String s1, String s2) {

    String prefix = "";

    int limit = (s1.length() < s2.length()) ? s1.length() : s2.length();

    for (int i = 0; i < limit; i++) {

      char ch1 = s1.charAt(i);
      char ch2 = s2.charAt(i);

      String temp = commonPrefix(ch1, ch2);
      if (temp == null)
        break;

      prefix += temp;
    }
    return prefix;
  }

  public static String commonPrefix(char ch1, char ch2) {

    if (ch1 == ch2)
      return "" + ch1;

    return null;
  }
}



PreviousNext

Related