Question

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?

Was it helpful?

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

OTHER TIPS

Since this is for school, i'm sure you can't go crazy with more advanced topics. Lets keep it simple with a switch/case.

You can map the letters to their corresponding number first, just use a switch/case to find the correct number depending on the letter.

For example:

String phoneNumber = "1800ab";

for(int x=0; x < phoneNumber.Length; x++)
{
   if(Char.IsLetter(phoneNumber[x]))
   {
      switch(phoneNumber[x].ToString().ToLower())
      {
         case "a":
         case "b":
         case "c":
           //This is number 2!
         break;


      }
   }
}

String already implements IEnumerable<char> - so no need to "break up" there.

Mapping of something to something (like letter code to matching number) is generally done with map (associative array) types (in C#/.Net it is Dictionary) that provide mapping one value ("key") to corresponding "value" - consider using that.

string letter1 = AskuserforInput("first letter");
string number1 = SwitchMethod(letter1);

string letter2 = AskuserforInput("second letter");
string number2 = SwitchMethod(letter2);

string letter3 = AskuserforInput("third letter");
string number3 = SwitchMethod(letter3);

string letter4 = AskuserforInput("fouth letter");
string number4 = SwitchMethod(letter4);

string letter5 = AskuserforInput("fifth letter");
string number5 = SwitchMethod(letter5);

string letter6 = AskuserforInput("sixth letter");
string number6 = SwitchMethod(letter6);

string letter7 = AskuserforInput("seventh letter");
string number7 = SwitchMethod(letter7);

string letter8 = AskuserforInput("eigth letter");
string number8 = SwitchMethod(letter8);

string letter9 = AskuserforInput("ninth letter");
string number9 = SwitchMethod(letter9);

string letter10 = AskuserforInput("tenth letter");
string number10 = SwitchMethod(letter10);
//declaring strings





Console.WriteLine("This is the original letter phone digits");
Console.WriteLine("({0}{1}{2})) {3}{4}{5} - {6}{7}{8}{9} ", letter1,letter2, letter3, letter4, letter5, letter6, letter7, letter8, letter9, letter10);//continue this


    Console.WriteLine("The actual numbers" );
    Console.WriteLine("({0}{1}{2})) {3}{4}{5} - {6}{7}{8}{9} ", number1, number2, number3, number4, number5, number6, number7, number8, number9, number10);//continue this
    Console.Read();

    #region End Program
    //wait for program to acknowledge results
    Console.BackgroundColor = ConsoleColor.White;
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("\n\nPlease hit ENTER to end program. . .");
    Console.Read();
    #endregion
    Console.Read();
    //also pulled this back up from a previous program


}

public static string SwitchMethod(string x)
{
    string y = "*";
    switch (x)
    {
        case "0":
            y = "0";
            break;
        case "1":
            y = "1";
            break;
        case "A":
        case "a":
        case "B":
        case "b":
        case "C":
        case "c":
        case "2":
            y = "2";
            break;

        case "D":
        case "d":
        case "E":
        case "e":
        case "F":
        case "f":
        case "3":
            y = "3";
            break;

        case "G":
        case "g":
        case "H":
        case "h":
        case "I":
        case "i":
        case "4":
            y = "4";
            break;

        case "J":
        case "j":
        case "K":
        case "k":
        case "L":
        case "l":
        case "5":
           y = "5";
            break;

        case "M":
        case "m":
        case "N":
        case "n":
        case "O":
        case "o":
        case "6":
            y = "6";
            break;

        case "P":
        case "p":
        case "Q":
        case "q":
        case "R":
        case "r":
        case "S":
        case "s":
        case "7":
            y = "7";
            break;


        case "T":
        case "t":
        case "U":
        case "u":
        case "V":
        case "v":
        case "8":
            y = "8";
            break;

        case "W":
        case "w":
        case "X":
        case "x":
        case "Y":
        case "y":
        case "Z":
        case "z":
        case "9":
            y ="9";
            break;
        default:
            Console.WriteLine("knucklehead, not a letter");
            Console.WriteLine("an '*' will show up");
            break;
            //used cases, next will use to.lower
            //Lynch helped

    }
    return y;


}


public static string AskuserforInput(string x)
{
    Console.WriteLine("\nPlease type {0}", x);
    String input = Console.ReadLine();
    return input;
}

I'm sure someone can think of a better way, but you could loop through each digit and pass it to this function:

int Asc(char ch)
{    
//Return the character value of the given character    
  return (int)Encoding.ASCII.GetBytes(ch)[0];
}

Then just assign a number based on which ASCII character is returned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top