What makes the 'getCharNumber' method case-insensitive while it only every checks for lowercase (by author of CtCI)

public class Common {      public static int getCharNumber(Character c) {         int a = Character.getNumericValue('a');         int z = Character.getNumericValue('z');                  int val = Character.getNumericValue(c);         if (a <= val && val <= z) {             return val - a;         }         return -1;     }          public static int[] buildCharFrequencyTable(String phrase) {         int[] table = new int[Character.getNumericValue('z') - Character.getNumericValue('a') + 1];         for (char c : phrase.toCharArray()) {             int x = getCharNumber(c);             if (x != -1) {                 table[x]++;             }         }         return table;     } } 

Above algorithm was used for testing whether a string is a permuation of a palindrome and was authored by CtCI (Cracking the Coding Interview).

My question: Why is the getCharNumber method case-insensitive?

I thought it should to be case-sensitive as it only checks for lowercase characters.

Add Comment
1 Answer(s)

Why is the getCharNumber case-insensitive?

The getCharNumber method uses Java’s Character#getNumericValue(char) method for which its JavaDoc states in particular:

The letters A-Z in their uppercase (‘\u0041’ through ‘\u005A’), lowercase (‘\u0061’ through ‘\u007A’), and full width variant (‘\uFF21’ through ‘\uFF3A’ and ‘\uFF41’ through ‘\uFF5A’) forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.

Meaning that for example for character A and a this API method returns the same value, i.e. 10, and thus there’s no case-sensitivity.


For reference, see also

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.