1

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?

user2505650
  • 1,293
  • 6
  • 20
  • 38
  • 3
    By busting out your friendly IDE and writing some code yourself. Try that. Then if you have issues come ask a question. – eddie_cat Jun 19 '14 at 19:01
  • Yes i have already tried some code but it is not working i will post it on an edit – user2505650 Jun 19 '14 at 19:03
  • Granted, this is pretty basic. That being said, not every new programmer (especially if they didn't go to school) is going to recognize that characters are just numbers in a table. – BradleyDotNET Jun 19 '14 at 19:07
  • I didn't say that because it's basic; I said it because it's so basic that Googling it and trying something would have taken less time than asking a question here. http://stackoverflow.com/questions/20044730/convert-character-to-its-alphabet-integer-position – eddie_cat Jun 19 '14 at 19:11
  • 1
    That code has to got to be the most convoluted way I have ever seen to try and solve this problem. Just take advantage of the ASCII table as in the answers :) – BradleyDotNET Jun 19 '14 at 19:15
  • Though I haven't looked at it close enough, it looks like your code should work if you just remove the `else { return 896; }`. – ispiro Jun 19 '14 at 19:18
  • Still after I remove the else {return 896; } I have the error that function does not always return a result how can I fix this? – user2505650 Jun 19 '14 at 19:24
  • @user2505650 Put it right after the closing bracket that's after it = after the `foreach`'s closing bracket. – ispiro Jun 19 '14 at 19:26
  • it still doesnt work... – user2505650 Jun 19 '14 at 19:29
  • By the way - Your method, though not optimal in other ways, has the advantage of not being dependent on the characters being stored in sequence. This is probably inconsequential, but is a good programing habit - not to just assume things! – ispiro Jun 19 '14 at 19:29
  • 1
    @user2505650 OK. So I tested now - You have to just `return 896;` after the `foreach`'s closing bracket. Without the `else { }`. And the input should be a lowercase letter only. Tested. Works. You also have to add a `03` in `Ranks`. And change the `25` in the initialization to `26`. – ispiro Jun 19 '14 at 19:34
  • i am sorry i tried to add the return after the foreach but i still have the same error can you please paste your version ? – user2505650 Jun 19 '14 at 19:46

3 Answers3

4

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).

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • i would like to code it myself without using any such libraries – user2505650 Jun 19 '14 at 19:08
  • 1
    @user2505650, by "libraries" do you mean the `Convert` class? Its basically just doing a cast. If you cant cast, then I'm not sure what you are going to do... – BradleyDotNET Jun 19 '14 at 19:10
  • @user2505650 Would (int)'A' be any better? That is basically what it is doing... You can go look up the source for Convert on line to see exactly what it does. C# has great libraries for hiding that detail from you, why the restriction? – BradleyDotNET Jun 19 '14 at 19:19
  • Note that originally I said ASCII. Convert.ToInt32 takes a *unicode* character. The position of "A" is the same in both however. – BradleyDotNET Jun 19 '14 at 19:22
  • I try by curiosity to see how much I can code without using any libraries – user2505650 Jun 19 '14 at 19:25
  • @user2505650, but casting isn't using a library. Its just a language feature. I abstracted that cast (or whatever it actually does) through a convenient (and more readable) function call. Is casting really a problem? – BradleyDotNET Jun 19 '14 at 19:27
2
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.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Someone has downvoted every answer. I guess because they don't like the question? The only problem I have with your answer is that it lacks an explanation. Not worth a downvote though. – BradleyDotNET Jun 19 '14 at 19:05
  • Good explanation! I gave you my +1 for a case-insensitive solution. – BradleyDotNET Jun 19 '14 at 19:12
0

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"));
merlin2011
  • 71,677
  • 44
  • 195
  • 329