use a for loop to created this program add coments people sometimes give their t
ID: 3554552 • Letter: U
Question
use a for loop to created this program
add coments
people sometimes give their telephone numbers using one or more alphabetic characters. Wrtie a program that accepts a 10 digit telephone number that may contain one or more alphabetic characters. Display the corresponding number using numerals. the numbers and letters are associated as follows on your telephone: ABC: 2 DEF: 3 GHI: 4 JKL: 5 MNO: 6 PQRS:7 TUV:8 WXYZ:9 if the user enters a character that is not on the telephone as part of the number, display a message indication that the value entered does not mach a number. Allow both upper and lowercase letters to be entered
use a for loop to created this program
add coments
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication10
{
// stores the relationship between phone alphabet keys and numbers
public static class PhoneCharacters
{
public static string[] abc { get { return new string[] { "abc", "2" }; } }
public static string[] def { get { return new string[] { "def", "3" }; } }
public static string[] ghi { get { return new string[] { "ghi", "4" }; } }
public static string[] jkl { get { return new string[] { "jkl", "5" }; } }
public static string[] mno { get { return new string[] { "mno", "6" }; } }
public static string[] pqrs { get { return new string[] { "pqrs", "7" }; } }
public static string[] tuv { get { return new string[] { "tuv", "8" }; } }
public static string[] wxyz { get { return new string[] { "wxyz", "9" }; } }
public static string[] ValidCharacters
{
get
{
return new string[] {"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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
}
}
public static bool ValidateInput(string phone)
{
string character = "";
for (int i = 0; i < phone.Length; i++)
{
character = phone.Substring(i, 1);
if (!ValidCharacters.Contains(character.ToLower()))
{
return false;
}
}
return true;
}
public static string GetPhone(string phone)
{
string resolvedPhone = "";
string character = "";
for (int i = 0; i < phone.Length; i++)
{
character = phone.Substring(i, 1).ToLower();
if (PhoneCharacters.abc[0].Contains(character))
resolvedPhone += PhoneCharacters.abc[1];
else if (PhoneCharacters.def[0].Contains(character))
resolvedPhone += PhoneCharacters.def[1];
else if (PhoneCharacters.ghi[0].Contains(character))
resolvedPhone += PhoneCharacters.ghi[1];
else if (PhoneCharacters.jkl[0].Contains(character))
resolvedPhone += PhoneCharacters.jkl[1];
else if (PhoneCharacters.mno[0].Contains(character))
resolvedPhone += PhoneCharacters.mno[1];
else if (PhoneCharacters.pqrs[0].Contains(character))
resolvedPhone += PhoneCharacters.pqrs[1];
else if (PhoneCharacters.tuv[0].Contains(character))
resolvedPhone += PhoneCharacters.tuv[1];
else if (PhoneCharacters.wxyz[0].Contains(character))
resolvedPhone += PhoneCharacters.wxyz[1];
else
resolvedPhone += character;
}
return resolvedPhone;
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a phone number: ");
string phone = Console.ReadLine();
if (phone.Length != 10)
{
Console.WriteLine("Invalid input! The phone must contain 10 characters!");
return;
}
if (!PhoneCharacters.ValidateInput(phone))
{
Console.WriteLine("Invalid input! Incorrect symbol in the phone!");
return;
}
Console.WriteLine("The phone is: " + PhoneCharacters.GetPhone(phone));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.