user2505650
user2505650

Reputation: 1381

Convert a Letter to its alphabet numerical Rank

I am trying to convert a letter to its alphabet numerical order for example if I have an 'A' it will give me 00 or a 'C' 02

How can I code this in c# ?

EDIT : This is what I tried

I created this class :

   public class AlphabetLetter 
    {
        public char Letter {get; set;}
        public int Rank {get; set;} 
    }

Those Two Lists :

     public List<char> Letters = new List<char> {
  'a' ,'b' ,'c' ,'d' ,'e', 'f' ,'g' , 'h' ,  'i' , 'j' , 'k' , 'l' , 'm',
  'n' ,'o' ,'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z'     
    };
        public List<int> Ranks = new List<int> { 

        00,01,02,04,05,06,07,08,09,10,11,12,13,
        14,15,16,17,18,19,20,21,22,23,24,25

        };


    public List<AlphabetLetter> Alphabet = new List<AlphabetLetter>( );

I created the Alphabet in my Constructor :

for (int i = 0; i < 25; i++) 
           {
               Alphabet.Add(new AlphabetLetter { Rank = Ranks[i], Letter = Letters[i] });

And tried to match a char with this function :

   public  int   Numberize(char Letter)
       {

           if (Letter != null)
           {
               foreach (AlphabetLetter _letter in Alphabet)
               {

                   if (Letter == _letter.Letter)
                   {

                       return _letter.Rank;

                   }
                   else
                   {
                       return 896;
                   }
               }
           }
           else {

               return 999;
           }



       }
               }

But this method is not working and is too tedious.

Any suggestions?

Upvotes: 1

Views: 1770

Answers (3)

BradleyDotNET
BradleyDotNET

Reputation: 61379

You start by simply getting its Unicode value:

int charValue = Convert.ToInt32('A');

Then account for where 'A' is on the Unicode table (65)

int rank = charValue - 65;

Note that this won't work for lower case letters, as they are in a different position. You could use ToLower or ToUpper on the string version of the character to nullify this (as in the other answer).

Upvotes: 4

merlin2011
merlin2011

Reputation: 75649

You do not need any fancy conversion. Just subtract ascii A and add 1.

using System;
using System.IO;

public class P{
    public static void Main(string[] args) {
       var letter = 'C';
       Console.WriteLine(letter - 'A' + 1);
    }
}

If you want to pad with leading zeroes, use ToString with a format.

   Console.WriteLine((letter - 'A' + 1).ToString("00"));

Upvotes: 0

ispiro
ispiro

Reputation: 27743

string yourLetter = "C";
int i = yourLetter.ToLower().ToCharArray()[0] - 'a';

This returns 2.

An explanation: The characters as char's are in sequential order. However, there are two sequences - of uppercase, and of lowercase. So first we convert it to lowercase.

Then change it to a character (by using the built in method for turning a string into a character array, and then taking the first and only one).

Then, using the fact that c# will happily treat the char as a number, subtract the first of the sequence from it.

Upvotes: 2

Related Questions