classes II - uppercase

Hello!
Please, could someone help me with understanding, in the chapter classes II ofthis tutorial, how does the line:

 
  ...  element+='A'-'a';


work to achieve changing chars to uppercase?
many thanks!!!
(is it using ASCII somehow - I thought but am not sure??? )
Many thanks again!!!
Yes, it uses ASCII.

All English letters are numbered sequentially.

A=65, B=66, C=67, ...
a=97, B=98, C=99, ...

'a'-'A' calculates the distance between upper case 'A' and lower case 'a'. The distance between upper and lower case is the same for all letters so it doesn't matter if A or some other letter is used here.

If element is an upper case letter and you add the distance between upper and lower case letters you will get the lower case version of the same letter.

An easier way to convert letters to lowercase is to use the std::tolower function.
http://www.cplusplus.com/reference/cctype/tolower/
 
element = std::tolower(element);
Last edited on
Many thanks!!! completely clear now!!!! :)
Topic archived. No new replies allowed.