0

all.

I think I can only understand one way to make count[128] mark the string's characters' occurences:

if s string characters are all lowercase, then it comes to :

for(char c: s.toCharArray())  count[c-'a']++;

If they are all uppercase,

for(char c: S.toCharArray())  count[c-'A']++;

But I can't make sense how does this way to mark the character directly into int indexes?

for(char c: s.toCharArray())    count[c]++;

From my understanding, is it count['A'] making sense?

Rio Chen
  • 1
  • 7
  • `char` is a numerical type. That's why you can subtract from it. – shmosel Aug 01 '19 at 03:59
  • What are the indices which you actually want? What is the index for `a`? What about `A`? – Tim Biegeleisen Aug 01 '19 at 04:00
  • yes, it is making sense (as using subtraction in `c-'a'`) - `char` is an integral type ([JLS 4.2.1. Integral Types and Values](https://docs.oracle.com/javase/specs/jls/se12/html/jls-4.html#jls-4.2.1)) in Java so it can be used as an index or in arithmetics – user85421 Aug 01 '19 at 04:20
  • also see [this](https://stackoverflow.com/q/57265562/85421) asked yesterday and [this](https://stackoverflow.com/q/57226439/85421) and linked questions – user85421 Aug 01 '19 at 04:32
  • `From my understanding, is it count['A'] making sense` Yes, makes sense. Note that `'A'` is not the same as `"A"`. `'A'` is an integer type just like `int` and `long`. So it can be used anywhere an `int` can (with some provisos and exceptions for widening and narrowing operations). See the ASCII chart below, `count['A']` is the same as `count[65]` (on the chart below, A = 65). – markspace Aug 01 '19 at 05:57

2 Answers2

0

The int index represents that character's position in the ANSI character table.

d33j
  • 434
  • 3
  • 9
0

Each character actually a number and each number represents a symbol. Have a look at this table: enter image description here

Here : Character index [97-122] represents [A-Z] and [65-90] represents [a-z], also [48-57] represents ['0'-'9']. such as

'A' = 65
'B' = 66
'C' = 67
 ...
 ...
'Z' = 90

So count['a'] actually count[97], count['A'] actually count[65].

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37