01
private
void
numButton_Click(
object
sender, EventArgs e)
02
{
03
04
.
05
string
input = numTextBox.Text;
06
07
if
(IsValidNumber(input))
08
09
10
11
//TelephoneFormat(ref input);
12
MessageBox.Show(
" The number is "
+ Translate(input));
13
14
}
15
else
16
17
"Invalid input"
);
18
19
001
using
System;
002
System.Collections.Generic;
003
System.ComponentModel;
004
System.Data;
005
System.Drawing;
006
System.Linq;
007
System.Text;
008
System.Threading.Tasks;
009
System.Windows.Forms;
010
011
012
013
namespace
TelephoneNumberTranslator
014
015
public
partial
class
TelephoneNumberTranslator : Form
016
017
TelephoneNumberTranslator()
018
019
InitializeComponent();
020
021
022
023
// The isValidNumber method accepts a string and returns true if it
024
// contains 10 digits, or false otherwise.
025
bool
IsValidNumber(
str)
026
027
const
int
VALID_LENGTH = 10;
// Length of a valid string
028
valid =
true
;
// Flag to indicate validity
029
030
// Check the string's length.
031
(str.Length == VALID_LENGTH)
032
033
034
035
036
037
false
038
039
// Return the status
040
return
valid;
041
042
// The telephone format method accepts a string argument by reference and
043
// formats it as a telephone number.
044
TelephoneFormat(
ref
045
046
// First, insert the left paren at position 0.
047
str = str.Insert(0,
"("
048
049
// Next, insert the right paren at position 4.
050
str = str.Insert(4,
")"
051
052
// Next, insert the hyphen at position 8.
053
str = str.Insert(8,
"-"
054
055
056
// Creates Dictionary to hold values
057
readonly
Dictionary<
,
char
[]> keys =
058
new
[]>()
059
060
{1,
[] {}},
061
{2,
[] {
'a'
'b'
'c'
'A'
'B'
'C'
}},
062
{3,
'd'
'D'
'E'
'e'
'f'
'F'
063
{4,
'g'
'G'
'H'
'h'
'I'
'i'
064
{5,
'j'
'J'
'K'
'k'
'L'
'l'
065
{6,
'm'
'M'
'N'
'n'
'O'
'o'
066
{7,
'p'
'P'
'Q'
'q'
'R'
'r'
'S'
's'
067
{8,
't'
'T'
'U'
'u'
'V'
'v'
068
{9,
'w'
'W'
'X'
'x'
'Y'
'y'
'Z'
'z'
069
{0,
' '
070
071
};
072
073
// Translates strings into numbers
074
075
[] Translate(
word)
076
077
word.Select(c =>
078
keys.First(k => k.Value.Contains(c)).Key).ToArray();
079
080
081
082
083
084
085
086
// Get a trimmed copy of the user's input.
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103