Help on a Word-phonenumber conversion program.

Hi,in one of my tutorial assignments we were asked to make a program in 'C' that would translate a string of 7 or 10 character words into its corresponding number according to the layout of a phone pad. We were to write 3 functions as follows;

1. Write a function to check phone numbers where this function takes in a string representing the dictionary word and returns a string representing the appropriate phone number. The phone number should be set to the "empty" string if the word does not form a phone number.
2. Write another function that takes in a single character and returns the associated phone digit (as a character). For example, the character 'A' would return '2'. For this program, you are to ignore the case of the character (thus 'g' and 'G' would return 4. If the character does not appear as a number on the phone, your function is to return '0' to signal this fact.
3. Write the main function that should open the input file containing the words and the output file to store the output. It should then read every word from the input file (one at a time) and as it reads each word, call the function to check the phone number to see if it is convertible. If the word forms a valid phone number of 7 or 10 letters, print this information into the output file. Finally, print the percentage of valid phone numbers that can be derived (either to the output file or to the screen) and then close the output and input files.


Specifics:
-You must search through all the words in the stored in a file, checking to see which ones are valid phone numbers.
-You should use the strlen function to determine the length of each word.
-Each word is on a separate line of the file. You are required to use the fgets function to read each word (line).
-Your program should write (save) each word that corresponds to a number (followed by its number) into a file.
-You must count how many 7 or 10 letter words are in the file and how many of these words translate into phone numbers. You should print the percentage (to 1 decimal place) at the end of your program, either into a file or to the screen.

An example of the last couple of lines of you

Comments

  • : Hi,in one of my tutorial assignments we were asked to make a program
    : in 'C' that would translate a string of 7 or 10 character words into
    : its corresponding number according to the layout of a phone pad. We
    : were to write 3 functions as follows;
    :

    I'll post the most important parts. To complete the program, however, you'll need to do a fair amount of editing.
    Also, when you want to run parts, remember to include malloc.h and string.h, and for the file reading code stdio.h


    : 1. Write a function to check phone numbers where this function takes
    : in a string representing the dictionary word and returns a string
    : representing the appropriate phone number. The phone number should
    : be set to the "empty" string if the word does not form a phone
    : number.

    [code]
    char* toNumber(char* szWord)
    {
    /* TODO: Check if it's a valid word */

    /* Max 10 numbers, plus 0 termination */
    char* szNumber = (char*)malloc(11);

    for (i=0; i<strlen(szWord); i++)
    {
    switch(szWord[i])
    {
    case 'a':
    case 'b':
    case 'c':
    szNumber[i] = '1';
    break;

    case 'd':
    case 'e':
    case 'f':
    szNumber[i] = '2';
    break;

    ...
    }
    }
    /* Zero terminate */
    szNumber[i] = 0;

    /* TODO: Format phone number with '-' characters */

    return szNumber;
    }
    /* TODO: Make sure you free() the memory used by the return value */
    [/code]

    : 2. Write another function that takes in a single character and
    : returns the associated phone digit (as a character). For example,
    : the character 'A' would return '2'. For this program, you are to
    : ignore the case of the character (thus 'g' and 'G' would return 4.
    : If the character does not appear as a number on the phone, your
    : function is to return '0' to signal this fact.

    Pretty mutch what I did in the switch statement above.

    : 3. Write the main function that should open the input file
    : containing the words and the output file to store the output. It
    : should then read every word from the input file (one at a time) and
    : as it reads each word, call the function to check the phone number
    : to see if it is convertible. If the word forms a valid phone number
    : of 7 or 10 letters, print this information into the output file.
    : Finally, print the percentage of valid phone numbers that can be
    : derived (either to the output file or to the screen) and then close
    : the output and input files.
    :

    [code]
    FILE* file = fopen("C:\numbers.txt", "r");
    if (file == NULL)
    {
    /* TODO: File could not be opened */
    }

    char szIn[100];
    if (fgets(szIn, 100, file) != NULL)
    {
    /* Remove newline character from the end */
    szIn[strlen(szIn)-1] = '';
    /* TODO: Interpret as phone word */
    }

    fclose(file);
    [/code]
    Ofcourse, this code only reads one line (and assumes a line is no longer than 98 characters). You'll need to loop the fgets() part.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • Hi Richard, thanx for the reply.

    I did strain myself and pretty much picked a couple of things from you code but for the 'switch' am I supposed to continue all down till the last letter? thought there would be an easier way around it! Your last comment, "The way I see it... Well, it's all pretty blurry" has led my to believe that this program might be beyond my scope though I thought as a good practice one should try to push their limits.
    Would be glad if there is anything more to this because this program intrigued me!

    Thankx!
  • : Hi Richard, thanx for the reply.
    :
    : I did strain myself and pretty much picked a couple of things from
    : you code but for the 'switch' am I supposed to continue all down
    : till the last letter? thought there would be an easier way around
    : it!

    Yeah I am affraid so. Atleast, it was the first thing that came to mind.
    Alright, I'll think of something smarter.
    * makes an impressive face * Ahh, an idea:
    It's fairly straight-forward, until you reach the 'r' (there are 4 characters associated with 7) and also there are 4 characters associated with '9'. The if statements correct for this.
    After that, it's a simpel matter of checking how many multiples of three the character is above 'a', and adding that to the start of the phone word numbers: 2.
    [code]
    char ch = [italic]character to convert[/italic];

    /* Cast to lower case - see ASCII table why this works */
    /* What this does is insure that bit 5 is always set */
    /* (starting with the first bit being 0 and the last 7 */
    ch = ch | 32;

    if (ch == 'z') ch--;
    if (ch >= 's') ch--;

    int number = 2 + (int)((ch - 'a') / 3)
    [/code]
    I haven't tested it yet, but it should work ;)

    : Your last comment, "The way I see it... Well, it's all pretty
    : blurry" has led my to believe that this program might be beyond my
    : scope though I thought as a good practice one should try to push
    : their limits.

    Neh, it's just my signature ;)

    : Would be glad if there is anything more to this because this program
    : intrigued me!
    :
    I'd invite you to try and complete it. If ever you get stuck, you know the place to get help :)
    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories