+ 1

Is there any way to do this?

I know we can print any letter's ascii value. A-Z or a-z. Suppose you have given a number between 1-26, you have to print the exact uppercase letter at that number. A for 1, B for 2, H for 8, Z for 26 & so on... Is there any way to do this?

22nd Jun 2020, 5:08 AM
Ashfaq Reshad
Ashfaq Reshad - avatar
3 Answers
+ 3
Get the input number, make sure it's not less than 1 or more than 26. Then use the number as an offset from uppercase 'A'. char c = 'A' + (number - 1); Or you can use a char table like XXX suggested.
22nd Jun 2020, 5:27 AM
Gen2oo
Gen2oo - avatar
+ 3
A simple approach would be to assign the uppercase letters to a string and then use the number given to take the letter at that position from the string. Like this string upper_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; cout << upper_letters[0]; //outputs A cout << upper_letters[25]; // outputs Z
22nd Jun 2020, 5:28 AM
XXX
XXX - avatar
+ 2
(just descriping what Gen2oo is doing) add the input (number-1) to the ASCII value of "A" and you will get the ASCII value of desired alphabet, then type cast the answer to 'char' and display it. heres how👇 https://code.sololearn.com/c84SJI0LTHep/?ref=app
22nd Jun 2020, 6:45 AM
Arsenic
Arsenic - avatar