Search code examples
objective-calphabet

How to Convert Every Letter in the Alphabet To an Integer - Objective-C


I am trying to convert every letter in the alphabet to an integer like this

A = 1;
B = 2;
...
Z = 26;

I went through some forum questions but none of them worked well. Is there a way of doing this without Arrays?


Solution

  • for lowercase letters you need to subtract 96 from its value. Like this:

    int a = 'a' - 96; // 1
    

    for uppercase ones subtract 64. Like this:

    int A = 'A' - 64;// 1
    

    This will work for any letter of English alphabet