0

Subtracting chars?

Heyya, I don't understand this code: [...] for(int x = 0; x<Str.length(); x++) { int ch = Str.charAt(x) - 'A'; } [...] Why are they subtracting A

27th Feb 2019, 9:58 PM
Gino ^^
Gino ^^ - avatar
2 odpowiedzi
+ 3
'A' = 65 (note: 'A' is char, between single quotes) In the code above, is substracting 65 to every char in Str... Why? Perhaps to get the char order... Post the whole code and we'll find out...
27th Feb 2019, 10:53 PM
unChabon
unChabon - avatar
+ 3
Can you post the full code from code playground? Every letter has an ascii value. int ch = ascii value from the string at position x - ascii value from A For example: str = hello h = 104 e = 101 l = 108 l = 108 o = 111 A = 65 int ch = 104 - 65 = 39 //charAt (0) - A 101 - 65 = 36 //charAt (1) - A 108 - 65 = 43 //charAt (2) - A 108 - 65 = 43 //charAt (3) - A 111 - 65 = 43 //charAt (4) - A But I can not say what is the sense behind it. You could convert the ascii values back to char but you would get special characters (The alphabet starts with A = 65 and ends with z = 122) Ascci converter: https://code.sololearn.com/c51G49FaP3OC/?ref=app
27th Feb 2019, 11:02 PM
Denise Roßberg
Denise Roßberg - avatar