Is there a quick way to retrieve given character's position in the english alphabet in C?
Something like:
int position = get_position('g');
int position = 'g' - 'a' + 1;
In C, char
values are convertible to int
values and take on their ASCII values. In this case, 'a'
is the same as 97 and 'g'
is 103. Since the alphabet is contiguous within the ASCII character set, subtracting 'a'
from your value gives its relative position. Add 1 if you consider 'a'
to be the first (instead of zeroth) position.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With