classes II - uppercase

Apr 15, 2016 at 10:05am
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!!!
Apr 15, 2016 at 10:27am
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 Apr 15, 2016 at 10:30am
Apr 15, 2016 at 1:53pm
Many thanks!!! completely clear now!!!! :)
Topic archived. No new replies allowed.