Telephone Words

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Telephone
{
    class Telephone
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Write("Enter a telephone number: ");
                string phoneNumber = Console.ReadLine();
                //creating a jagged array for digit matrix
                char[][] digitMatrix = new char[phoneNumber.Length][];
                //initializing the jagged array
                for (int i = 0; i < phoneNumber.Length; i++)
                {
                    digitMatrix[i] = new char[GetString(int.Parse(phoneNumber.Substring(i, 1))).Length];
                    for (int j = 0; j < GetString(int.Parse(phoneNumber.Substring(i, 1))).Length; j++)
                    {
                        digitMatrix[i][j] = char.Parse(GetString(int.Parse(phoneNumber.Substring(i, 1))).Substring(j, 1));
                    }
                }
                StreamWriter sw = new StreamWriter(@"C:\word.txt", false);
                //Create the pointer array
                int[] pointer = new int[phoneNumber.Length];
                //Pointer increment loop
                pointer[phoneNumber.Length – 1]–;
                //logic to iterate through the digitMatrix
                do
                {
                    int i = phoneNumber.Length – 1;
                label1:
                    pointer[i]++;
                    if (pointer[i] == GetString(int.Parse(phoneNumber.Substring(i, 1))).Length && i != 0)
                    {
                        pointer[i] = 0;
                        i–;
                        goto label1;
                    }
                    else if (pointer[0] == GetString(int.Parse(phoneNumber.Substring(0, 1))).Length)
                    {
                        break;
                    }
                    else
                    {
                        //Print the matrix based on pointer array
                        for (int j = 0; j < phoneNumber.Length; j++)
                        {
                            Console.Write(digitMatrix[j][pointer[j]]);
                            sw.Write(digitMatrix[j][pointer[j]]);
                        }
                        Console.WriteLine();
                        sw.WriteLine();
                    }
                } while (pointer[0] != GetString(int.Parse(phoneNumber.Substring(0, 1))).Length);
                sw.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        public static string GetString(int digit)
        {
            switch (digit)
            {
                case 1: return "1";
                case 2: return "ABC";
                case 3: return "DEF";
                case 4: return "GHI";
                case 5: return "JKL";
                case 6: return "MNO";
                case 7: return "PQRS";
                case 8: return "TUV";
                case 9: return "WXYZ";
                case 0: return "0";
                default: return "Not a valid digit";
            }
        }
    }
}
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s