1. (60 points) Write a program that modifies a word by replacing every alphabeti
ID: 3701292 • Letter: 1
Question
1. (60 points) Write a program that modifies a word by replacing every alphabetical letter (lower case or upper case) with a digit (2-9). The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table:
Input: TAKEOUT
Output:8253688
Your program should include the following function:
voidconvert(char *word,char *numbers);
The function expects word to point to a string containing to be translated ; numbers represent sthe pointer pointing to the string storing the numbers. For example, if the is TAKEOUT the function will store 8253688 pointed by numbers.
1)Name your program word_numbers.c
2)Assume input is no longer than 1000characters.
3)The convert function should use pointer arithmetic (instead of arraysubscripting). In other words, eliminate the loop index variables and all use of the [] operator in the function.
4)To read a line of text, use theread_line function (the pointer version) in the lecture notes.
DigitLetter Digit Letter MNO PQRS TUV WXYZ ABC6 DEF 7 GHI JKL 10Explanation / Answer
Please find my implementation:
#include<stdio.h>
void convert(char *word, char *numbers);
int main()
{
int i;
char str[] = "TAKEOUT",numbers[10];
convert(str, numbers);
printf("Word %s to number: %s ", str,numbers);
}
void convert(char *word, char *numbers)
{
int i;
char c;
for (i = 0; i < strlen(word); i++)
{
//convert a letter to lower case
c = tolower(word[i]);
switch (c)
{
case 'a':
case 'b':
case 'c':
*(numbers + i) = 48 + 2;
break;
case 'd':
case 'e':
case 'f':
*(numbers + i) = 48 + 3;
break;
case 'g':
case 'h':
case 'i':
*(numbers + i) = 48 + 4;
break;
case 'j':
case 'k':
case 'l':
*(numbers + i) = 48 + 5;
break;
case 'm':
case 'n':
case 'o':
*(numbers + i) = 48 + 6;
break;
case 'p':
case 'q':
case 'r':
case 's':
* (numbers + i) = 48 + 7;
break;
case 't':
case 'u':
case 'v':
*(numbers + i) = 48 + 8;
break;
case 'w':
case 'x':
case 'y':
case 'z':
* (numbers + i) = 48 + 9;
break;
}
}
numbers[i] = '';
}
/*output
Word TAKEOUT to number: 8253688
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.