By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Sifting Phone Numbers

P: 3
I am currently in doing a program which is given by my tutor:

Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are "special" because they are easily translated into words. You've been asked to create a list of phone numbers that are directly mappable to words by searching a dictionary for every 7 or 10 letter word that maps on to the phone lettering scheme:

ABC - "2" DEF - "3"

GHI - "4" JKL - "5" MNO - "6"

PRS - "7" TUV - "8" WXY - "9"

Given these letters, the word xylophones is really the phone number 995-674-6637
The word, yipping is the phone number 947-7464

Specifics:
1. You will write three functions, detailed below.
2. You must search through all the words in the stored in a file, checking to see which ones are valid phone numbers.
3. You should use the strlen function to determine the length of each word.
4. Each word is on a separate line of the file. You are required to use the fgets function to read each word (line).
5. Your program should write (save) each word that corresponds to a number (followed by its number) into a file.
6. 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’re the output would be:
...
word yesteryear is the phone number 937-837-9327
word yipping is the phone number 947-7464
word yttrium is the phone number 988-7486
96.8 percent of the 7/10 letter words that were translatable into phone numbers

7. You will be using a string parameter for one of your functions. A string is just an array of characters. When you don't know how long an array parameter will be (how many values it will contain), you can use empty [] to represent the fact that it is an array of unknown length. Thus function(char my_string[]) would be a function that takes a string of indeterminate length.

8. Your main function should return 0 upon completion.


Required functions:

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.

Can anyone help me?
Aug 28 '07 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
No. You have an assignment from a tutor and I will not do your homework for you.

Please read the posting guidelines.
Aug 28 '07 #2

P: 3
No. You have an assignment from a tutor and I will not do your homework for you.

Please read the posting guidelines.
I had already done halfway, just no idea to continue:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #include <string.h>
  4.  
  5. #define SIZE 100
  6.  
  7.  
  8.  
  9. void numberChecker( void );
  10.  
  11. void printNumber ( int *ptr, int length );
  12.  
  13. int digitConverter( char );
  14.  
  15.  
  16.  
  17. int main()
  18.  
  19. {
  20.  
  21. numberChecker();
  22.  
  23.  
  24. return 0;
  25.  
  26. }
  27.  
  28.  
  29.  
  30. void numberChecker( void )
  31.  
  32. {
  33.  
  34. char dictionaryWord[ SIZE ];
  35.  
  36. int phoneNumber[ 10 ];
  37.  
  38. int *ptr = phoneNumber;
  39.  
  40. char letter;
  41.  
  42. int length, i;
  43.  
  44.  
  45. printf( "Please enter a word: " );
  46.  
  47. scanf( "%s", dictionaryWord);
  48.  
  49.  
  50. length = ( int )strlen( dictionaryWord );
  51.  
  52.  
  53. for(i = 0; i < length; i++) {
  54.  
  55. letter = dictionaryWord[ i ];
  56.  
  57. phoneNumber[ i ] = digitConverter( letter );
  58.  
  59. //printf( "%d", phoneNumber[ i ] );
  60.  
  61. }
  62.  
  63. printNumber( ptr, length );
  64.  
  65.  
  66. }
  67.  
  68.  
  69.  
  70. void printNumber( int *ptr, int length )
  71.  
  72. {
  73.  
  74.  
  75. int j;
  76.  
  77. for( j = 0; j < length; j++ ) {
  78.  
  79. printf("%d", ptr[ j ] );
  80.    }
  81.  
  82. }
  83.  
  84.  
  85. int digitConverter( char alphabets )
  86.  
  87. {
  88.  
  89. int digit;
  90.  
  91.  
  92. switch( alphabets )
  93.  
  94.  
  95. {
  96.  
  97.       case 'A':
  98.       case 'a':
  99.       case 'B':
  100.       case 'b':
  101.       case 'C':
  102.       case 'c':
  103.          d
  104. igit = 2;
  105.          break;
  106.  
  107.  
  108. case 'D':
  109.       case 'd':
  110.       case 'E':
  111.       case 'e':
  112.       case 'F':
  113.       case 'f':
  114.  
  115. digit = 3;
  116.          break;
  117.  
  118.  
  119. case 'G':
  120.       case 'g':
  121.       case 'H':
  122.       case 'h':
  123.       case 'I':
  124.       case 'i':
  125.          digit = 4;
  126.          break;
  127.  
  128.  
  129. case 'J':
  130.       case 'j':
  131.       case 'K':
  132.       case 'k':
  133.       case 'L':
  134.       case 'l':
  135.          digit = 5;
  136.          break;
  137.  
  138.  
  139. case 'M':
  140.       case 'm':
  141.       case 'N':
  142.       case 'n':
  143.       case 'O':
  144.       case 'o':
  145.          digit = 6;
  146.          break;
  147.  
  148.  
  149. case 'P':
  150.       case 'p':
  151.       case 'R':
  152.       case 'r':
  153.       case 'S':
  154.       case 's':
  155.          digit = 7;
  156.          break;
  157.  
  158.  
  159. case 'T':
  160.       case 't':
  161.       case 'U':
  162.       case 'u':
  163.       case 'V':
  164.       case 'v':
  165.          digit = 8;
  166.          break;
  167.  
  168.  
  169. case 'W':
  170.       case 'w':
  171.       case 'X':
  172.       case 'x':
  173.       case 'Y':
  174.       case 'y':
  175.          digit = 9;
  176.          break;
  177.  
  178.  
  179.    return digit;
  180.  
  181. }
Please help me!
Aug 28 '07 #3

RRick
Expert 100+
P: 463
It looks like you have the logic done to take a word and print out a the phone number.

What's next? Read the word from a file instead of scanf. Take a look at fopen and fgets. These will do what you need.

I would suggest putting the file reading code in main and once you get a word, just pass the word to numberchecker.
Aug 28 '07 #4

P: 3
It looks like you have the logic done to take a word and print out a the phone number.

What's next? Read the word from a file instead of scanf. Take a look at fopen and fgets. These will do what you need.

I would suggest putting the file reading code in main and once you get a word, just pass the word to numberchecker.
#include <stdio.h>
#include <string.h>
#define SIZE 20

char* numberChecker( char word[] );
char digitConverter( char alphabets );

int main( void )

{

char *words;
char *noPtr;

int count = 0;
int totalCount = 0;

FILE *dfPtr;
FILE *ofPtr;

dfPtr = fopen( "dictionary.txt", "r" );
ofPtr = fopen( "output.txt", "w" );

if( dfPtr == NULL ) {
printf( "Error! File could not be opened for input.\n" );
}

else if ( ofPtr == NULL ) {
printf( "Error! File could not be opened for output.\n" );
}

else {
while( fgets( words, SIZE, dfPtr ) != NULL ) {

noPtr = numberChecker( words );


if( noPtr[ 7 ] == '\0' ) {
fprintf( ofPtr, "The word %s is the phone number %c%c%c-%c%c%c%c\n",
words, noPtr[ 0 ], noPtr[ 1 ], noPtr[ 2 ], noPtr[ 3 ],
noPtr[ 4 ], noPtr[ 5 ], noPtr[ 6 ] );
count++;
}

else if( noPtr[ 10 ] == '\0' ) {
fprintf( ofPtr, "The word %s is the phone number %c%c%c-%c%c%c-%c%c%c%c\n",
words, noPtr[ 0 ], noPtr[ 1 ], noPtr[ 2 ], noPtr[ 3 ],
noPtr[ 4 ], noPtr[ 5 ], noPtr[ 6 ], noPtr[ 7 ], noPtr[ 8 ],
noPtr[ 9 ] );
count++;
}

else;

totalCount++;
}
}

fprintf( ofPtr, "\n%.1f percent of the 7/10 letter words were translatable into phone numbers\n",
(count / (float)totalCount) * 100.0);

printf("%.1f percent of the 7/10 letter words were translatable into phone numbers\n",
(count / (float)totalCount) * 100.0);

fclose( dfPtr );
fclose( ofPtr );

return 0;
}


char* numberChecker( char word[] )
{
int length;
int i;
static char phNumber[ SIZE ];
char letter;

length = strlen( word ) - 1;

if( length == 7 || length == 10 ) {
for(i = 0 ; i < length; i++ ) {
letter = word[ i ];
phNumber[ i ] = digitConverter( letter );
}
phNumber[ length ] = '\0';
}

else
phNumber[ 0 ] = '\0';

return phNumber;
}


char digitConverter( char alphabets )
{
char digit;

switch( alphabets )

{

case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
digit = '2';
break;

case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
digit = '3';
break;

case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
digit = '4';
break;

case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
digit = '5';
break;

case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
digit = '6';
break;

case 'P':
case 'p':
case 'R':
case 'r':
case 'S':
case 's':
digit = '7';
break;

case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
digit = '8';
break;

case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
digit = '9';
break;

default:
digit = '0';
break;
}

return digit;
}

but the output gives non-fatal error. can anyone gives me some hints?
Aug 31 '07 #5

RRick
Expert 100+
P: 463
First of all. put [ code] .... [/code] around your source code.

Second, you're going to have have to help us with the error/problem. What kind of error are you getting from the output? Also, include how us the code you think might be affected (you don't need to show everything).
Aug 31 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.