assigning an int value to a char

I need to assign each letter of the alphabet to a value, starting with ‘a’ = 0; ‘b’ = 1,…, ‘z’ = 25. I could obviously go through and just do it one by one, but I am trying to figure out how to do it using a loop (or multiple loops if necessary). It seems like it should be easy, but I can’t seem to put it together.

I haven’t really tried much yet because I am having a hard time thinking of what I can try. I think I just need a couple tips on how to get started and usually once I get started and have the idea in my head I can figure it out from there.

>Solution :

c - ‘a’ 

will do what you want
assuming c is lowercase.

This subtracts the ascii value of ‘a’
from that of the variable c,
giving the distance from ‘a’

Leave a Reply