Phone number to word conversion program

This is a discussion on Phone number to word conversion program within the C Programming forums, part of the General Programming Boards category; I don't usually do this bc I can usually learn code from the internet but I am having trouble with ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    13

    Exclamation Phone number to word conversion program

    I don't usually do this bc I can usually learn code from the internet but I am having trouble with this problem and I can't seem to find any help. Here it goes:
    Write a C program that, given a seven-digit number, writes to a file every possible seven-letter word (does not have to be a real word, no dictionaries required) corresponding to that number, i.e. 2=abc 3=def, and so on. There are 2187 such words for each phone numer. Avoid using digits 1 and 0. Note: letters in the output file are uppercase. Do not accept phone numbers with a zero or one. When the user runs the program, prompt them for a phone number. If it is invalid, exit with an appropriate comment.
    Thank you everyone!
    Trevor

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    And what is your question? We do not do homeworks, we answer questions to help you to do it yourself
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    13

    Question phone number to word conversion

    i have a couple codes that might work for the phone number to word conversion but i don't know how I would export these into a text file.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char buf[BUFSIZ], *p;
    
      printf("Enter phone number: ");
      fflush(stdout);
    
      fgets(buf,sizeof buf,stdin);
    
      for (p = buf;*p;p++)
      {
        /* print out each character here, *p */
        switch (*p)
        {
          case 'a':
          case 'b':
          case 'c':
            printf("2");
            break;
          case 'd':
          case 'e':
          case 'f':
            printf("3");
            break;
          case 'g':
          case 'h':
          case 'i':
            printf("4");
            break;
          case 'j':
          case 'k':
          case 'l':
            printf("5");
            break;
          case 'm':
          case 'n':
          case 'o':
            printf("6");
            break;
          case 'p':
          case 'q':
          case 'r':
          case 's':
            printf("7");
            break;
          case 't':
          case 'u':
          case 'v':
            printf("8");
            break;
          case 'w':
          case 'x':
          case 'y':
          case 'z':
            printf("9");
            break;
    
        }
      }
    
      return 0;
    }
    or

    Code:
    #include <stdio.h>
    
    int main(){
    
    int ch;
    int i,res;
    char buff[BUFSIZ];
    
    printf ("Enter a phone number: ");
    fgets(buff,sizeof(buff),stdin);
    for(i=0;buff[i]!='\0';i++)
    {
     ch=buff[i];
    
    if ('a' <= ch && ch <= 'c')
     res = 2;
    if ('d' <= ch && ch <= 'f')
     res = 3;
    if ('g' <= ch && ch <= 'i')
     res = 4;
    if ('j' <= ch && ch <= 'l')
     res = 5;
    if ('m' <= ch && ch <= 'o')
     res = 6;
    if ('p' <= ch && ch <= 's')
     res = 7;
    if ('t' <= ch && ch <= 'v')
     res = 8;
    if ('w' <= ch && ch <= 'y')
     res = 9;
    
    printf ("&#37;d ", res);
    
    }
    getchar();
    return 0;
    }
    Last edited by lostmyshadow; 12-03-2007 at 12:26 AM.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    please edit your message: add code tags around your code.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    13

    ok

    ok i put code tags around it

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. buffer does not contans chars - it contains numbers like:

    9733675

    you should scan it and transform to word like:
    wpddmpj

    This word should be put into file.
    Then next word should be generated fro the number like:
    wpddmpk
    wpddmpl
    wpddmqj
    wpddmqk
    wpddmql
    wpddmrj

    etc
    and each generated word should be stored in the file till you get the last possible word:
    zsffosl

    if during converting number to work you encounter 0 or 1 - you should stop the work and inform the user that number is incorrect. The same for too short or too long numbers
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    13

    but..

    that is helpful but that i what i am getting hung up on, how do i generate multiple words from the 7 digit phone number? I think both of those programs i posted are backwards. i think those are character to phone number generators instead of phone number to name convertor which is what i am trying to do. sorry about the confusion.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    So given say 4 digits 9675, you want to derive the words "WORK" and/or "YORK" ?
    In fact, all possible words you could make?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The difficult part of this imo, is that any phone number MIGHT exclude some numbers - so you can't just do a "give me all possible combinations of letters", type of function, and have it be accurate.

    I see two ways around that problem:

    1) Go ahead and generate all possible answers, but before final output, test each answer to ensure it has only letters that are valid.

    Using the string functions here, might be considered.

    2) Incorporate logic that will skip invalid letter combinations, during the generation of combinations.

    Another way to do this, beside using string functions, would be to make an "include" array, of int's, and have it hold a 1 value for every letter that is valid, and a 0 value for every letter that is not valid. Then reference that array to see if any particular letter should be included or not, in a combination.

    I like the "include" array, myself, but either will work fine.

    Do you have the pseudo-code for the algorithm to solve this, yet? You may want to do a bit of this with paper and pencil by hand, to get the hang of it, before you try to write out your pseudo-code.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    I assume that by "invalid letter combinations", you mean sequences like ZNQL which don't spell a proper word?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, I thought all letter combo's were OK, in that sense. What I meant was if the phone number didn't have a '2' in it, then A,B, & C would be three letters that should be excluded from any possible combination answer.

    So the array would have something like this for a phone number of "23":

    0 1 2 3 4 - element #
    =======
    0 0 1 1 0 - value

    Now that I think about it more, I believe he needs every *permutation* of the letters from the phone number, and not just every *combination* of letters. Sound right?
    Last edited by Adak; 12-03-2007 at 03:24 PM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'd like to bump this in case LostMyShadow is still interested.

    Fun little program, and not as easy as I thought it would be.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Thumbs up Phone number to string conversion using recursion

    Code:
    // PhoneNumberToString.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    static int COUNT = 0;
    
    char Letters[][4] = {
    	{'0'},
    	{'1'},
    	{'a', 'b', 'c'},
    	{'d', 'e', 'f'},
    	{'g', 'h', 'i'},
    	{'j', 'k', 'l'},
    	{'m', 'n', 'o'},
    	{'p', 'q', 'r', 's'},
    	{'t', 'u', 'v'},
    	{'w', 'x', 'y', 'z'}
    };
    
    void ConvertPhoneNumberToString(char phone[], int nArraySize, int nStart)
    {
    	if(nStart < nArraySize - 1)
    	{
    		char chDigit = phone[nStart];
    		if(chDigit != '-')
    		{
    			int nDigit = chDigit - '0';
    			for(int iLetterCount=0; iLetterCount < 4; ++iLetterCount)
    			{
    				char chLetter = Letters[nDigit][iLetterCount];
    				if(chLetter != 0)
    				{
    					phone[nStart] = chLetter;
    					ConvertPhoneNumberToString(phone, nArraySize, nStart + 1);
    					phone[nStart] = chDigit;
    				}
    			}
    		}
    		else
    		{
    			ConvertPhoneNumberToString(phone, nArraySize, nStart + 1);
    		}
    	}
    	else
    	{
    		COUNT++;
    		printf("[%d]=> %s\n", COUNT, phone);
    		
    	}
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char phone[] = {'5','0','2','-', '8','7','6','-','2','3','4','5', '\0'};
    	
    	ConvertPhoneNumberToString(phone, sizeof(phone)/sizeof(char), 8);
    	char ch;
    	scanf("%c", &ch);
    	return 0;
    }
    
    From
    --Ashish Patel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program that requests input of an integer number
    By theejuice in forum C Programming
    Replies: 6
    Last Post: 04-30-2008, 02:18 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Relocation in .obj -files
    By willkoh in forum C++ Programming
    Replies: 6
    Last Post: 04-06-2005, 01:59 PM
  4. registering a phone number
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-08-2004, 10:15 PM
  5. Replies: 3
    Last Post: 01-14-2003, 09:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21