PDA

View Full Version : [SOLVED] Convert Letters In Alphabet to numerical position (C)


DoctorZeus
28th August 2011, 09:41 PM
Hello,

I had written a really useful C/C++ header file containing a function a while back (which I lost somewhere) which if you passed a char into it would return the letter (A-Z) position in the alphabet as an integer (1-26). I somehow did this without a load of nested 'if' statements but for some reason can't for the life of me remember how I did it...

E.G. A = 1, B =2, Z = 26.

Does anyone have any suggestions on how to achieve this?

Thanks

DoctorZeus

---------- Post added at 09:41 PM ---------- Previous post was at 09:30 PM ----------

Sorry all! I just remembered the ASCII standard! (Side effect of working under the .Net framework for too long grrr):

For anyone who was curious:

int CharLetterToNumber(char CInput)
{
return CInput - 'A'+1;
}