Counting each letter in an array with an ASCII table (Unicode code)

I am new to Java and I could not understand this structure:

public static int[] upperCounter(String str) {
    final int NUMCHARS = 26;
    int[] upperCounts = new int[NUMCHARS];
    char c;

    for (int i = 0; i < str.length(); i++) {
        c = str.charAt(i);
        if (c >= 'A' && c <= 'Z')
            upperCounts[c-'A']++;
    }

    return upperCounts;
}

This method works, but what does it mean list[c-'A']++;?

+4
source share
4 answers

c - 'A'takes a character in a range ['A' .. 'Z']and subtracts 'A'to create a numeric value in the range [0 .. 25], so it can be used as an index of an array.

upperCounts[c - 'A']++increases the number of errors for a character cusing its corresponding index c - 'A'.

In fact, the loop generates an array of character type values.

+5
source

, (++) c - 'A' list.

c - -

'A' (65 ). B 66 ..

+3

, :

c - 'a'

c - , 'a' - , , ASCII. .

list[c - 'a']

int[], n- list, .

list[c - 'a']++;

++ .

+3

char . A 65, A 97 (. ASCII ).

list[c-'A']++;

, c ( 65 90 [ if ( c >= 'A' && c <= 'Z' )]) A (.. 65). list .

: c is c:

C = 67
A = 65
C - A = 2

2 . 2 , , c .

+2

Source: https://habr.com/ru/post/1568831/


All Articles