C++ Summary To make telephone numbers easier to remember, some companies use let
ID: 3755468 • Letter: C
Question
C++
Summary
To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN.
In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letters.
Instructions
Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits.
If the user enters more than seven letters, then process only the first seven letters.
Also output the - (hyphen) after the third digit.
Allow the user to use both uppercase and lowercase letters as well as spaces between words.
Moreover, your program should process as many telephone numbers as the user wants.
The program should accept input and produce output similar to the example program execution below:
Enter Y/y to convert a telephone number from
letters to digits.
Enter any other letter to terminate the program.
Y
Enter a telephone number using letters: Hello world
The corresponding telephone number is:
435-5696
To process another telephone number, enter Y/y
Enter any other letter to terminate the program.
z
Test Cases:
1.
Input
Y
Programming
Z
Expected Output
776-4726
2.
Input
Expected Output
266-5263
3.
Input
Expected Output
347-7868 732-6636
Explanation / Answer
#include <iostream>
using namespace std;
int main(){
int counter;
char phoneNumber;
char ans='';
cin >> ans;
while(ans=='Y'||ans=='y')
{
for(counter = 0; counter < 7; counter ++)
{
cin >> phoneNumber;
if(counter == 3)
cout << "-";
if(phoneNumber >= 'A' && phoneNumber <= 'Z'
|| phoneNumber >= 'a' && phoneNumber <= 'z')
switch(phoneNumber)
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
cout << 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
cout << 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
cout << 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
cout << 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
cout << 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
cout << 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
cout << 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
cout << 9;
break;
}
}
cin >> ans;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.