Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that takes a phone number (3-digit extension number) as an input

ID: 3691045 • Letter: W

Question

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 1 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”, 1 is “NULL”, 2 is “ABC”, 3 is “DEF”, …) IN C CODE PLEASE

1 ABC I DEF 2 3 GHI JKL MNo 4 PQRS I TUV I(WXYZ 8 9 OOU Figure source (Wikipedia)

Explanation / Answer

#include <stdio.h>
#include <string.h>

// hashTable[i] stores all characters that correspond to digit i in phone
const char hashTable[10][5] = {"", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"};

// A recursive function to print all possible words that can be obtained
// by input number[] of size n. The output words are one by one stored
// in output[]
void printWordsUtil(int number[], int curr_digit, char output[], int n)
{
// Base case, if current output word is prepared
int i;
if (curr_digit == n)
{
printf("%s ", output);
return ;
}

// Try all 3 possible characters for current digir in number[]
// and recur for remaining digits
for (i=0; i<strlen(hashTable[number[curr_digit]]); i++)
{
output[curr_digit] = hashTable[number[curr_digit]][i];
printWordsUtil(number, curr_digit+1, output, n);
if (number[curr_digit] == 0 || number[curr_digit] == 1)
return;
}
}

// A wrapper over printWordsUtil(). It creates an output array and
// calls printWordsUtil()
void printWords(int number[], int n)
{
char result[n+1];
result[n] ='';
printWordsUtil(number, 0, result, n);
}

int checkAndGetArray(int number[], int n){
   int temp = n%100;
   number[0] = n/100;
   number[1] = temp/10;
   number[2] = n%10;
   int i;
   for(i=0; i<3; i++)
       if(number[i]==0 || number[i]==1){
           printf("Invalid input! The number should mot contain 0 or 1 ");
           return 0;
       }
   return 1;
}
//Driver program
int main(void)
{
   int n;
   printf("Enter a number: ");
   scanf("%d", &n);
   int number[3];
   if(!checkAndGetArray(number, n))
       return 0;


printWords(number, 3);
return 0;
}

/*

sample run:

Enter a number: 234
adg adh adi aeg aeh aei afg afh afi bdg bdh bdi beg beh bei bfg bfh bfi cdg cdh cdi ceg ceh cei cfg cfh cfi

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote