Complete the following projects:

1. 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:
Many people find it difficult to memorize phone numbers, so they use the
correspondence between digits and letters to develop seven-letter words that
correspond to their phone numbers. For example, a person whose telephone number
is 686-2377 might use the correspondence indicated in the above table to develop the
seven-letter word “NUMBERS.”
Businesses frequently attempt to get telephone numbers that are easy for their clients
to remember. If a business can advertise a simple word for its customers to dial, then
no doubt the business will receive a few more calls. Each seven-letter word
corresponds to exactly one seven-digit telephone number. The restaurant wishing to
increase its take-home business could surely do so with the number 825-3688 (i.e.,
“TAKEOUT”). Each seven-digit phone number corresponds to many separate sevenletter words. Unfortunately, most of these represent unrecognizable juxtapositions of
It is possible, however, that the owner of a barber shop would be pleased to
know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A
veterinarian with the phone number 738-2273 would be pleased to know that the
number corresponds to “PETCARE.”

Write a program that, given a seven-digit number, writes to a file every possible sevenletter word corresponding to that number. User will provide the file name and
extension. There are 2187 (3 to the seventh power) such words. Avoid phone numbers
with the digits 0 and 1.
Assume that the file created above is now your local dictionary. Now modify your code
to look up the words in the dictionary. Some seven-letter combinations created by your
program consist of two or more words (e.g., the phone number 843-2677 produces
“THEBOSS”). User will provide the word and your program can respond positively if
the word is found.

1. In some programming languages, strings are entered surrounded by either single or
double quotation marks. Write a program that reads the three strings john, ‘john’,
“john”. Are the single and double quotes ignored by C or read as part of the string?
Now write a new method called clean_quotes that will remove all single quotes and
double quotes. Note: the program should NOT remove commas or apostrophe s.

Solution

q1.c

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

/* a helper function to make sure the phone number is a valid 7 digits */

voidcheck_phone_number(char phone[]);

/* this function generates the words and saves them into the given file */

void generate(char filename[], char phone[]);

/* generate the whole word recursively */

voidgenerate_char(FILE *file, char phone[], char word[], int index, int length);

/* search the word in the file.*/

int search(char filename[], char word[]);

int main() {

char phone[128];

char word[128];

char filename[128];

/* prompt the user to enter the phone number */

printf(“Enter the 7-digit phone number (no 0 or 1): “);

scanf(“%s”, phone);

check_phone_number(phone);

/* prompt the user to enter the name of file to save result */

printf(“Enter the name of the file to save results: “);

scanf(“%s”, filename);

/* generate the words into the specified file */

generate(filename, phone);

printf(“The words are saved into %s.\n\n”, filename);

/* prompt the user to enter the word to search in the file */

printf(“Enter the uppercase word to search in the file: “);

scanf(“%s”, word);

/* search the entered word in the file */

if (search(filename, word)) {

printf(“`%s` found!\n”, word);

} else {

}

return 0;

}

voidcheck_phone_number(char phone[]) {

int i;

if (strlen(phone) != 7) {

printf(“The phone number must be 7 digits (no 0 and 1).\n”);

exit(-1);

}

/* make sure each character must be a digit */

for (i = 0; phone[i] != ‘\0’; i++) {

if (phone[i] < ‘2’ || phone[i] > ‘9’) {

printf(“The phone number must be 7 digits (no 0 and 1).\n”);

exit(-1);

}

}

}

void generate(char filename[], char phone[]) {

FILE *file;

char word[8];

/* open the file in write mode to save the words */

file = fopen(filename, “w”);

/* generate the word one by one using the recursive function */

word[7] = ‘\0’;

generate_char(file, phone, word, 0, strlen(phone));

fclose(file);

}

voidgenerate_char(FILE *file, char phone[], char word[], int index, int length) {

/* this array stores the mapping between digits and alphabets.

* for example mappings[2] is “ABC”. */

static char *mappings[] = {“”, “”, “ABC”, “DEF”, “GHI”, “JKL”, “MNO”, “PQRS”, “TUV”, “WXYZ” };

if (index == length) {

fprintf(file, “%s\n”, word);

} else {

int offset = phone[index] – ‘0’;

char *mapping = mappings[offset];

/* try each mapping of the current digit */

while (*mapping != ‘\0’) {

word[index] = *mapping;

generate_char(file, phone, word, index + 1, length); /* check next digit */

mapping ++;

}

}

}

int search(char filename[], char word1[]) {

FILE *file;

char word2[128];

int found = 0;

/* open the file in read mode */

file = fopen(filename, “r”);

/* read word by word until the end of file,

* and compare them with the given word */

while (fscanf(file, “%s”, word2) == 1) {

if (strcmp(word1, word2) == 0) {

found = 1;

break;

}

}

fclose(file);

return found;

}

q1_output.txt

Enter the 7-digit phone number (no 0 or 1): 4247288

Enter the name of the file to save results: m.txt

The words are saved into m.txt.

Enter the uppercase word to search in the file: HAIRCUT

`HAIRCUT` found!

q2.c

#include <string.h>

#include <stdio.h>

/* this function removes single or double quotes. */

voidremove_quotes(char word[]);

int main() {

char word[32];

/* ask the user to enter three formats and display them.

* From the output, we can see that C read single and double quotes as part

* of the string. */

printf(“Enter john: “);

scanf(“%s”, word);

printf(“You just entered: %s\n\n”, word);

printf(“Enter ‘john’: “);

scanf(“%s”, word);

printf(“You just entered: %s\n\n”, word);

/* remove quotes and display again */

remove_quotes(word);

printf(“After quotes removed: %s\n\n”, word);

printf(“Enter \”john\”: “);

scanf(“%s”, word);

printf(“You just entered: %s\n\n”, word);

/* remove quotes and display again */

remove_quotes(word);

printf(“After quotes removed: %s\n\n”, word);

return 0;

}

/* this function removes single or double quotes. */

voidremove_quotes(char word[]) {

int i, j;

for (i = 0, j = 0; word[j] != ‘\0’; j++) {

char c = word[j];

if (c != ‘\” && c != ‘”‘) {

/* the current character is not a quote, save it back

* to the word */

word[i] = c;

i ++;

}

}

word[i] = ‘\0’;

}

q2_output.txt

Enter john: john

You just entered: john

Enter ‘john’: ‘john’

You just entered: ‘john’

After quotes removed: john

Enter “john”: “john”

You just entered: “john”

After quotes removed: john