Please help with this question phone_list.txt : Standard telephone keypads conta
ID: 3865192 • Letter: P
Question
Please help with this question
phone_list.txt :
Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table Digit Letter Digit Letter 2 ABC 6 MINTO DER D7 BPRS 4 GHI 8 TUV Write a program that translate seven-letter words in a file to their corresponding phone numbers 1. Name your program phone numbers. C 2. The program reads the content of the file and translate the seven-letter words into their corresponding phone numbers, then writes the phone numbers to a file with the same name but an added extension of .cvt. For example, if the original file name is phone list. txt, then the corresponding phone numbers will be stored in a file named phone list.txt.cvt. Assume the file name is no more than 100 characters. Enter the file name phone list. txt Output file name: phone list. txt.cvt 3. The program should include the following function: void translate (char word, char *phone number) The function expects word to point to a string containing the seven-letter words to be translated; phone number represents the pointer pointing to the string storing the phone number. For example, if the word is TAKEOUT, the function will store 8253688 pointed by phone numberExplanation / Answer
For "0" and "1" i'm assuming no action will be taken. The following is your code. It will write to phone_list.txt.cvt file and to the screen also.
code:
#include<stdio.h>
#include<string.h>
char *symbol[10]={"0","1","ABC","DEF","GHI","JKL","MNO","PRS","TUV","WXY"};
void translate(char *word, char * phone_number)
{
char mystr[2]="";
int len=strlen(word);
for(int i=0;i<len;i++)
{
mystr[0]=word[i];
for(int j=2;j<10;j++)
{
//printf("%s, %s ", symbol[j],mystr);
if(strstr(symbol[j],mystr)!=NULL)
{
phone_number[i]=j+48; //converting to ASCII
break;
}
}
}
phone_number[7]='';
}
int main(void)
{
FILE *fp1, *fp2;
char infile[100], outfile[100];
printf("Enter input file:");
scanf("%s", infile);
strcpy(outfile,infile);
strcat(outfile,".cvt");
char word[8];
char phone_number[8];
fp1=fopen(infile,"r");
fp2=fopen(outfile,"w");
if(fp1==NULL || fp2== NULL)
{
printf("Error in opening files ");
return 1;
}
while(fscanf(fp1, "%s", word)!=EOF){
translate(word,phone_number);
printf("%s ", phone_number);
fprintf(fp2,"%s ", phone_number);
}
fclose(fp1);
fclose(fp2);
return 0;
}
output:
Enter input file:phone_list.txt
8253688
4247288
8432677
2337226
7382273
2279274
6284457
2224245
4766637
2532637
3292273
2477678
6862377
7673596
5388377
6873867
7764726
7836574
5963729