+ 2
Manav Roy the character 'H' (not the string) is stored as an ASCII value, 72. Just cast it as an (int) to convert it, or just assign it. E.g., int num = 'H';. If you want its numeric index in the alphabet, then subtract 'A' from it. E.g., int num = 'H' - 'A'; The string "H" is made up of two characters, 'H' and '\0'. If you cast the string to (int) then you get an unexpected number because it copies 4 bytes into the int. The 72 is in the high byte, then the null, then two other bytes beyond. EDIT: After answering, I tried this on Sololearn and found that the C++ compiler rejects it, whereas it is permitted in C.
8th Feb 2022, 4:30 PM
Brian
Brian - avatar
+ 1
Without a numeric digit it is not considered a number. It is a shortcoming of stoi() that I believe has been there since the beginning of C.
8th Feb 2022, 4:02 PM
Brian
Brian - avatar