How do I pass integers to characters (in a string) in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I pass integers to characters (in a string) in C++?

I need to get, for example, the numerical value for a name. Let's take a random name; Axel. (Taking a=1 , b=2, c=3, ... , z=26) The numerical value should be 1+24+5+12=42 (a+x+e+l) Anyone can help?

18th Mar 2019, 6:56 AM
Aliah EH
Aliah EH - avatar
2 Answers
+ 8
I would do it like so: Look into the ascii table. http://www.asciitable.com/mobile/ Note the values that the uppercase or lowercase begins. Decide if you want to convert the users input into uppercase or lower case. Get the users input. Traverse the string using a for loop Subtract the starting number of the equivalent character set (upper or lower) from the characters ascii value plus 1 add this value to a running sum variable. Repeat as required until the string is complete.
18th Mar 2019, 7:32 AM
jay
jay - avatar
+ 1
Thanks a lot. I've been able to write the code and it runs perfectly :D
18th Mar 2019, 1:31 PM
Aliah EH
Aliah EH - avatar