USING C PLEASE MULTIPLE IF-ELSE Branching and Multiple if-else if (condition) st
ID: 3745558 • Letter: U
Question
USING C PLEASE
MULTIPLE IF-ELSE Branching and Multiple if-else if (condition) statements; else if (condition) statements else i (condition) statements; 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. else statements 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 1- 800-before each telephone number. 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 Pseudocode First!Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Code
#include<stdio.h>
#include<ctype.h>
//go through the comments, and you can write pseudocode on your own
//method to convert letters in input array to telephone numbers
//and store in output array
void convert(char *input, char *output){
int i=0; //index
char letterCount=0; //count of letters
//loops until input array is completely read or 7 letters are read
while(input[i]!='' && letterCount<7){
//getting character
char c=input[i];
c=toupper(c); //converting to upper case to reduce comparisons
//checking for each key code
//ABC - 2
//DEF - 3
//GHI - 4
//JKL - 5
//MNO - 6
//PQRS - 7
//TUV - 8
//WXYZ - 9
//white space - '-' (hyphen)
if(c=='A' || c=='B' || c=='C'){
output[i]='2';
//incrementing letter count
letterCount++;
}else if(c=='D' || c=='E' || c=='F'){
output[i]='3';
letterCount++;
}else if(c=='G' || c=='H' || c=='I'){
output[i]='4';
letterCount++;
}else if(c=='J' || c=='K' || c=='L'){
output[i]='5';
letterCount++;
}else if(c=='M' || c=='N' || c=='O'){
output[i]='6';
letterCount++;
}else if(c=='P' || c=='Q' || c=='R' || c=='S'){
output[i]='7';
letterCount++;
}else if(c=='T' || c=='U' || c=='V'){
output[i]='8';
letterCount++;
}else if(c=='W' || c=='X' || c=='Y' || c=='Z'){
output[i]='9';
letterCount++;
}else if(c==' '){
//if white space in encountered, adding a hyphen
output[i]='-';
//also no letter incrementing
}else{
//other than above cases, no change is made
output[i]=input[i];
}
i++; //moving to next index
}
//appending termination string to end of output array
output[i]='';
}
int main(){
//defining arrays to store input and output text
char input[20];
char output[20];
char choice='y'; //loop controller
//looping as long as user wish to continue
while(choice=='y' || choice=='Y'){
//prompting and getting input text
printf("Enter a telephone number in letters: ");
//following statement allows entry of text including white space,
//terminated by newline character
scanf("%[^ ]%*c", input);
//converting text to telephone number
convert(input,output);
//displaying it
printf("telephone number in digits: 1-800-%s ",output);
//prompting if the user wishes to continue
printf("Do you want to do this again? (y/n): ");
scanf("%c",&choice);
//clearing newline character out of buffer
fflush(stdin);
}
return 0;
}
//OUTPUT
Enter a telephone number in letters: GET LOAN
telephone number in digits: 1-800-438-5626
Do you want to do this again? (y/n): y
Enter a telephone number in letters: get loan
telephone number in digits: 1-800-438-5626
Do you want to do this again? (y/n): y
Enter a telephone number in letters: FUN TALK
telephone number in digits: 1-800-386-8255
Do you want to do this again? (y/n): y
Enter a telephone number in letters: LONG LONG LONG Text
telephone number in digits: 1-800-5664-566
Do you want to do this again? (y/n): n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.