Zoro
Zoro

Reputation: 719

Converting values with Arabic numbers

The problem states "Write a program that accepts a 10-digit telephone number that may contain one or more alphabetic characters. Display the corresponding number using numerals...etc" ABC:2 through WXYZ:9

This chapter teaches about loops but I found myself really lost at this problem. I completed the code but I think it sucks...

My question: Is there a better way to shorten this code up? And I only figured to use the c# keyword case, is there another way?

EDIT: Arabic as in you could type in 1800WALLTO and it will give you 1800925586

ALSO I am not asking for a code that doesn't work, this does EXACTLY what I want and asked it to do. I am just asking for any advise or input on how to make it better. I really wanted to know a way to do it without switch and case break etc...

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        int x = 0;
        char userInput = ' ';
        string inputString = "",
        outputString = "";

        Console.WriteLine("Enter the digits from the phone number");
        do
        {
            userInput = Console.ReadKey(false).KeyChar;
            inputString += userInput;
            if (Char.IsLetter(userInput))
                userInput = userInput.ToString().ToUpper().ToCharArray()[0];
            switch (userInput)
            {
                case '1':
                    outputString += '1';
                    x++;
                    break;
                case '2':
                case 'A':
                case 'B':
                case 'C':
                    outputString += '2';
                    x++;
                    break;
                case '3':
                case 'D':
                case 'E':
                case 'F':
                    outputString += '3';
                    x++;
                    break;
                case '4':
                case 'G':
                case 'H':
                case 'I':
                    outputString += '4';
                    x++;
                    break;
                case '5':
                case 'J':
                case 'K':
                case 'L':
                    outputString += '5';
                    x++;
                    break;
                case '6':
                case 'M':
                case 'N':
                case 'O':
                    outputString += '6';
                    x++;
                    break;
                case '7':
                case 'P':
                case 'Q':
                case 'R':
                case 'S':
                    outputString += '7';
                    x++;
                    break;
                case '8':
                case 'T':
                case 'U':
                case 'V':
                    outputString += '8';
                    x++;
                    break;
                case '9':
                case 'W':
                case 'X':
                case 'Y':
                case 'Z':
                    outputString += '9';
                    x++;
                    break;
                case '0':
                    outputString += '0';
                    x++;
                    break;
                default:
                    Console.WriteLine("You entered an incorrect value-Try again");
                    x--;
                    break;
            }

        }
        while (x < 10);
        Console.WriteLine("\nYou entered {0}", inputString);
        Console.WriteLine("Your number is {0}", outputString);

    }
}

}

Upvotes: 1

Views: 1161

Answers (2)

James Anderson
James Anderson

Reputation: 27478

Using a string look up would shorten the code:--

   String decode "--------------------------------01234567890------2223334445556667778889999---------------------------------"; //256 char string

  numout = decode.substring((int) Char.GetNumericValue(userinput),1);

But it would be a lot less efficient than using a "case" statement. Less code does not mean less cpu.

Upvotes: 0

theodox
theodox

Reputation: 12218

Use a System.Collections.Generic.Dictionary where the dictionary keys are the characters A-Z and 0-9 and the values are the corresponding numbers:

var lookup = Dictionary<char, int> { 
      {'A',2},
      {'B',2},
       // etc...
      {'Z', 9},
      {'1':1},
      {'2':2}
      // etc... include the numerals so you don't have to converts some things to char not the rest...
      };

  // to lookup  a character:
  char item = 'A';
  int number = lookup['A'];

To decode an phone number just split it into an array of char's and look them up one after the other

  List<int> digits = new List<int>();
  foreach (char c in inputString)
  { 
      digits.Add(lookup[c]);
  }

Im sure somebody will post a 1-liner using LINQ as well, but this is the vanilla version.

Upvotes: 1

Related Questions