473,390 Members | 900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Sifting Phone Numbers

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
5 3269
weaknessforcats
9,208 Expert Mod 8TB
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
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
463 Expert 256MB
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
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
463 Expert 256MB
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

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

Similar topics

5
by: joemono | last post by:
Hello everyone! First, I appologize if this posting isn't proper "netiquette" for this group. I've been working with perl for almost 2 years now. However, my regular expression knowledge is...
4
by: Brian Henry | last post by:
I have phone numbers like this in a data table 123-435-1234 1231231234 432.234.2321 they all have different formatting, what I want to do is get them all formatted like this (123) 123-1234
4
by: Earl | last post by:
I'm curious if there are others who have a better method of accepting/parsing phone numbers. I've used a couple of different techniques that are functional but I can't really say that I'm totally...
10
by: JackM | last post by:
I'm still working on validating the phone numbers that are entered on a form but have come across a problem I don't understand how to fix. I can handle most instances when it's in regular US...
3
by: venu | last post by:
Hi, I have a different requirement and it is : I need to validate a phone number field. It may or may not be a US phone number. The constraints are : *********************** # It should...
10
by: Petr Jakeš | last post by:
I have a standard 12-key mobile phone keypad connected to my Linux machine as a I2C peripheral. I would like to write a code which allows the text entry to the computer using this keypad (something...
4
by: lilOlMe | last post by:
I'd love to be able to validate phone numbers in my software but my product is being used world wide. Not everyone's phone number is formatted like USA/Canada formats theirs. I've found a few...
4
by: Blue Streak | last post by:
Hello, Folks! Does anyone know of a website that lists the local phone number formats for each country? TIA...
7
by: Propoflady | last post by:
My contacts can have as many as five or six phone numbers - is it possible to make a query that puts the name, and each phone number after it on the same line - for this reason I have two tables -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.