Using C: QUESTION 3. (10 points) Write a program that takes a phone number (3-di
ID: 3691218 • Letter: U
Question
Using C:
QUESTION 3. (10 points) Write a program that takes a phone number (3-digit extension number) as an input and prints all the phrases that correspond to it on the phone keypad. The figure below shows the telephone keypad and the corresponding letters for each digit. Since the digits l and 0 don't have letters associated with them, we'll rule out numbers that have 1 or 0. For such cases, print an "invalid input" message For a valid input, the program should print all the possible combinations that correspond to the number The output sample below is an example. The user would then go over these words and find a catchy word that they can use instead of their phone extension number. In the output below, the user's phone extension is 726; they can use instead the phrases (PAM, PAN, RAM, RAN, SAM, SAN). Hint: You can use a 2D string to store the original data such as: 0 is "NULL", I is "NULL", 2 is "ABC", 3 is "DEF. ...) 1 ABC DEF 2 3 GHI JKL MNO 4 PQRS I TUV I(WXYZ 8 9 Figure source (Wikipedia) Below are output samples:Explanation / Answer
// createhashTable[i]
The createhashtable array will storee all characters that correspond to digit i in phone
const char createhashTable[8][5] = ( "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"};
//The following is a a recursive function which will print all possible words that can be obtained by inputing inputnumber[] of size n. The output words will be stored in the array outputwords[] one by one
void toprintfinalwords(int inputnumber[], int current_digit, char outputwords[], int n)
{
// This is the base case, if current output word is prepared
int i;
if (current_digit == n)
{
printf("%s ", outputwords);
return ;
}
// Now we will try all 3 possible characters for current digit in inputnumber[]
and perform the recursion for remaining digits
for (i=0; i<strlen(createhashTable[inputnumber[current_digit]]); i++)
{
outputwords[current_digit] = createhashTable[inputnumber[current_digit]][i];
toprintfinalwords(inputnumber, current_digit+1, outputwords, n);
if (inputnumber[current_digit] == 0 || inputnumber[current_digit] == 1)
return;
}
}
This function will help us to creates an output array
void toprintwordcombinations(int inputnumber[], int n)
{
char resulthere[n+1];
resulthere[n] ='';
toprintfinalwords(inputnumber, 0, resulthere, n);
}
int main(void)
{
int inputnumber[] = {7, 5, 6}; // array to take in 3 digit extension numbe
//Or we can take input from the user
for(int i=0;i<3;i++)
cin>>inputnumber[i];
int n = sizeof(inputnumber)/sizeof(inputnumber[0]);
toprintwordcombinations(inputnumber, n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.