C# Convert Alphanumeric phone number

c#, console-application

Solution

There are certainly a lot of solutions here. Since you're already using Regex, you could approach it in a basic way:

num = Regex.Replace(num, @"[abcABC]", "2");
num = Regex.Replace(num, @"[defDEF]", "3");
//....

or you could create a `Dictionary<string,char>` and run through each char and convert it to the mapped character. Something like :

var dict = new Dictionary<string, char>(); 
dict.Add("abcABC",'2');
//...

foreach(char c in num.Where(char.IsLetter))
{
    var digit = dict.First(d => d.Key.Contains(c)).Value;
    num = num.Replace(c, digit);
} 

Like you said, the LINQ here is splitting the string to a char array, and looping through ones that are letters

Problem

I've been working on this issue for awhile and I've been stuck so I hope someone can push me in the right direction. I have a c# console application that will take in a string and verify that it contains only 0-9, a-z, A-Z, and -. My issue that I'm having is that I need to convert any letters in the phone number to their respective number. So if I input 1800-Flowers, it will output as 1800-3569377. I have my methods defined: I'm not looking for the solutions here (this is homework), but I'm looking for a push in the right direction. Do I need to convert the string to a char array to break up each individual character, and then use that in the convert method to switch any letter into a number?

Original source