|
| 1 | +package me.ramswaroop.bits; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by IntelliJ IDEA. |
| 5 | + * |
| 6 | + * @author: ramswaroop |
| 7 | + * @date: 6/15/15 |
| 8 | + * @time: 10:38 AM |
| 9 | + */ |
| 10 | +public class StrCmp { |
| 11 | + |
| 12 | + /** |
| 13 | + * Compares two strings {@param s1} and {@param s2} lexicographically ignoring case. |
| 14 | + * If both are equal, it returns 0 otherwise their lexicographic differences. |
| 15 | + * |
| 16 | + * @param s1 |
| 17 | + * @param s2 |
| 18 | + * @return |
| 19 | + */ |
| 20 | + public static int compareStringIgnoreCase(String s1, String s2) { |
| 21 | + int n1 = s1.length(); |
| 22 | + int n2 = s2.length(); |
| 23 | + int min = Math.min(n1, n2); |
| 24 | + for (int i = 0; i < min; i++) { |
| 25 | + char c1 = s1.charAt(i); |
| 26 | + char c2 = s2.charAt(i); |
| 27 | + if (c1 != c2) { |
| 28 | + // If characters don't match but case may be ignored, |
| 29 | + // try converting both characters to uppercase. |
| 30 | + // If the results match, then the comparison scan should |
| 31 | + // continue. |
| 32 | + c1 = Character.toUpperCase(c1); |
| 33 | + c2 = Character.toUpperCase(c2); |
| 34 | + if (c1 != c2) { |
| 35 | + // Unfortunately, conversion to uppercase does not work properly |
| 36 | + // for the Georgian alphabet, which has strange rules about case |
| 37 | + // conversion. So we need to make one last check before |
| 38 | + // exiting. |
| 39 | + c1 = Character.toUpperCase(c1); |
| 40 | + c2 = Character.toUpperCase(c2); |
| 41 | + if (c1 != c2) { |
| 42 | + // No overflow because of numeric promotion |
| 43 | + return c1 - c2; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + return n1 - n2; |
| 49 | + } |
| 50 | + |
| 51 | + public static void main(String a[]) { |
| 52 | + System.out.println(compareStringIgnoreCase("ram", "ram")); |
| 53 | + System.out.println(compareStringIgnoreCase("ram", "Ram")); |
| 54 | + System.out.println(compareStringIgnoreCase("", "")); |
| 55 | + System.out.println(compareStringIgnoreCase("", " ")); |
| 56 | + System.out.println(compareStringIgnoreCase(" ", " ")); |
| 57 | + System.out.println(compareStringIgnoreCase(" ", "")); |
| 58 | + System.out.println(compareStringIgnoreCase("Geeks", "apple")); |
| 59 | + System.out.println(compareStringIgnoreCase("", "ABCD")); |
| 60 | + System.out.println(compareStringIgnoreCase("ABCD", "z")); |
| 61 | + System.out.println(compareStringIgnoreCase("ABCD", "abcdEghe")); |
| 62 | + System.out.println(compareStringIgnoreCase("GeeksForGeeks", "gEEksFORGeEKs")); |
| 63 | + System.out.println(compareStringIgnoreCase("GeeksForGeeks", "geeksForGeeks")); |
| 64 | + System.out.println("--------------------"); |
| 65 | + System.out.println("ram".compareToIgnoreCase("ram")); |
| 66 | + System.out.println("ram".compareToIgnoreCase("Ram")); |
| 67 | + System.out.println("".compareToIgnoreCase("")); |
| 68 | + System.out.println("".compareToIgnoreCase(" ")); |
| 69 | + System.out.println(" ".compareToIgnoreCase(" ")); |
| 70 | + System.out.println(" ".compareToIgnoreCase("")); |
| 71 | + System.out.println("Geeks".compareToIgnoreCase("apple")); |
| 72 | + System.out.println("".compareToIgnoreCase("ABCD")); |
| 73 | + System.out.println("ABCD".compareToIgnoreCase("z")); |
| 74 | + System.out.println("ABCD".compareToIgnoreCase("abcdEghe")); |
| 75 | + System.out.println("GeeksForGeeks".compareToIgnoreCase("gEEksFORGeEKs")); |
| 76 | + System.out.println("GeeksForGeeks".compareToIgnoreCase("geeksForGeeks")); |
| 77 | + } |
| 78 | +} |
0 commit comments