• C

Word Generator

I need to develop a program which will ask the user to enter a seven digit phone number and then based on this number display all the possible word generated out comes.However, not including 0 and 1.

Eg. digit 2 gives the letters A B C.
What Im not sure about 100%  is how I should change the numbers to alphabet corresponding numbers.

I am not particularly strong in C but have developed some code and so forth but it seems to have some problems.







LushousAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnondCommented:
more info please. how exactly do you translate the digits to numbers ?

Arnon David.

P.S., if this is homework, we here at EE are forbiden to do it for you, we can only help in specific problems you might have.
0
LushousAuthor Commented:
Edited text of question.
0
LushousAuthor Commented:
Edited text of question.
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

arnondCommented:
do you have any algorithm for the conversion ? any written stuff on how to convert ?

Arnon David.
0
LushousAuthor Commented:
Arnon, im converting using a switch statement and then the following as my cases -
 case '2':
 alph[idx] = 'A';
 gen_alpha(num, alph, (idx+1) );
 alph[idx] = 'B';
 gen_alpha(num, alph, (idx+1) );
 alph[idx] = 'C';
 gen_alpha(num, alph, (idx+1) );
 break;
 And so on until I reach the number nine and its correspondants.
0
rbrCommented:
Sorry I still not understand the connection between 2 and A B C.
What should be the output for 3?
0
LushousAuthor Commented:
Ok, well basically I have it all planned Im just not to strong in C and unsure as I cant get this to work yet.

For 3 I would follow with the digits: D, E, F

Then for 4: G, H ,I

And so on except that I skip the letter Q along the way.
0
arnondCommented:
i don't understad it either. please give a few more examples for input\output.

Arnon David.
0
LushousAuthor Commented:
*I know what you mean, I dont quite get it myself. Basically one of the ohter experts has been helping me and said that that  code would work. However, I am having problems understanding it.

Basically I ask the user to enter a seven digit number and then based on the numbers within the entered number I display all the possible alphabetical outcomes.
Eg. plumber - 7586637

The above code Im not sure how it works but obviously it says that for number 2 the letters which correspond to it are A,B,C

For number 3 the letters corresponding are D, E,F

And so follows until we reach number nine which is the last number in the set and also the last alphabet letters, W, X, Y.
0
rbrCommented:
Oh you are talking about the chars on a telephon pad.

2=ABC
3=DEF
4=GHI
5=JKL
6=MN
7=PRS
8=TUV
9=WXY
0=OQZ

So try This code

char *keys[10]={"OQZ","","ABC","DEF","GHI","JKL","MN","PRS","TUV","WXY"};

void make_key (char *num, char *key, int depth)
{
      int i;
      for (i=0;i<strlen(keys[num[depth]]);i++) {
          key[depth]=keys[num[depth]][i];
          if (depth < strlen (num)+1) {
               make_key (num,key,depth+1);
          } else {
              key[depth+1]=0;
                  printf ("%s\n",key);
          }
      }
}
0
rbrCommented:
To start use

char key[10];

make_key ("26082",key,0);
0
LushousAuthor Commented:
Thats quite good rbr. Without asking too much would it be possible for you to comment this code so I know whats happening and why and where etc.

That way I also know what I need to add.

I appreciate all your help!
0
LushousAuthor Commented:
The keys should be -
2   ABC
3   DEF
4   GHI
5   JKL
6   MNO
7   PRS
8   TUV
9   WXY
0
rbrCommented:


                                              char *keys[10]={"","","ABC","DEF","GHI","JKL","MNO","PRS","TUV","WXY"};
/* These are the keys
pos 0 = "" no key
pos 1 = "" no key
pos 2 = "ABC"
..
..
..
*/
                                             void make_key (char *num, char *key, int depth)
/* num string of the number you want to convert
 * key current key generated
 * depth depth of the generation
 */
                                             {
                                              int i;
                                              for (i=0;i<strlen(keys[num[depth]]);i++) {
                                                  key[depth]=keys[num[depth]][i];
/* Place i. char of key reprensentation of num at pos depth to to key */
                                                  if (depth < strlen (num)+1) {
/* Not finished. Go one position further */                                                 make_key (num,key,depth+1);
                                                  } else {
  /* finished. Terminate the key and print it */                                                          key[depth+1]=0;
                                              printf ("%s\n",key);
                                                  }
                                              }
0
LushousAuthor Commented:
Thanks alot rbr for writing in the comments it is a little clearer to me now.I appreciate all your help.

However, without running this I assume that this code will work for just just one digit at a time..??

And thus I need to repeat this code another six times?????

Or will this do the job for the whole program??
0
LushousAuthor Commented:
Oh, and I noticed that above there is a commenmt from you rbr that tells me to start use

char key[10];

make_key ("26082",key,0);


what does this mean?? Do i still need to use this?? What is this for???

Thanks!


0
LushousAuthor Commented:
I have been playing wioth this program all day but cant get it to work!!!!!!!
0
LushousAuthor Commented:
rbr could you help me I have the following program done but its not working-
#include <stdio.h>
#include <string.h>
// Required for string function

// Function Declarations
void make_key (char *num, char *key, int depth);
// num string of the number you want to convert key current key generated  dep


main()
{
//Variable initalistaion for numbers
 char *keys[10]={"","","ABC","DEF","GHI","JKL","MNO","PRS","TUV","WXY"};
 

int i, num;

printf("Please enter a seven digit     telephone number: \n");
scanf("%d", &num);


for (i = 0; i < strlen(keys[num[depth]]); i++) {
   
key[depth]=keys[num[depth]][i];
       
if (depth < strlen (num)+1) {
   make_key (num,key,depth+1);
}
             
else {
 key[depth+1]=0;
          printf ("%s\n",key);
            }
       }  
}


Note - For the output, we should have about 1000 words or so, will this be able to be generated and displayed on the screen?????

0
LushousAuthor Commented:
Rbr Ive rejected your answer in the hope that you will be able to help me further, as ive indicated above.

Im knew to this site and noticed that it said that my question was locked so in the event that that would mean that you could not longer see this question ive rejected your answer.

That way you can see that I still needs your help.

However, I know that you've given me the answer and I do intend to accept this later.
0
rbrCommented:
Sorry I was of work when i got yopur comments.try This
#include <stdio.h>
                                          #include <string.h>
void make_key (char *num, char *key, int depth);
char *keys[10]={"","","ABC","DEF","GHI","JKL","MNO","PRS","TUV","WXY"};
 /* These are the keys
                                          pos 0 = "" no key
                                          pos 1 = "" no key
                                          pos 2 = "ABC"
                                          ..
                                          ..
                                          ..
                                          */
                                       
int main (int argn, char *argv[])
{
char num[10];
vhar key[10];
int number

printf ("Enter number ?");
scanf ("%d",&number);
sprintf (num,"%d",number);
make_keys(num,key,0);
return (1);
}
                                                                                                                                                                           void make_key (char *num, char *key, int depth)
                                          /* num string of the number you want to convert
                                           * key current key generated
                                           * depth depth of the generation
                                           */
                                                                                       {
                                                                                        int i;
                                                                                        for (i=0;i<strlen(keys[num[depth]]);i++) {
                                                                                            key[depth]=keys[num[depth]][i];
                                          /* Place i. char of key reprensentation of num at pos depth to to key */
                                                                                            if (depth < strlen (num)+1) {
                                          /* Not finished. Go one position further */                                                 make_key (num,key,depth+1);
                                                                                            } else {
                                            /* finished. Terminate the key and print it */                                                          key[depth+1]=0;
                                                                                        printf ("%s\n",key);
                                                                                            }
                                                                                        }

if you enter 2 the output should be
A
B
C
if you enter 23 the output should be
AD
AE
AF
BD
BE
BF
CD
CE
CF
0
rbrCommented:
Sorry I was of work when i got yopur comments.try This
#include <stdio.h>
                                          #include <string.h>
void make_key (char *num, char *key, int depth);
char *keys[10]={"","","ABC","DEF","GHI","JKL","MNO","PRS","TUV","WXY"};
 /* These are the keys
                                          pos 0 = "" no key
                                          pos 1 = "" no key
                                          pos 2 = "ABC"
                                          ..
                                          ..
                                          ..
                                          */
                                       
int main (int argn, char *argv[])
{
char num[10];
vhar key[10];
int number

printf ("Enter number ?");
scanf ("%d",&number);
sprintf (num,"%d",number);
make_keys(num,key,0);
return (1);
}
                                                                                                                                                                           void make_key (char *num, char *key, int depth)
                                          /* num string of the number you want to convert
                                           * key current key generated
                                           * depth depth of the generation
                                           */
                                                                                       {
                                                                                        int i;
                                                                                        for (i=0;i<strlen(keys[num[depth]]);i++) {
                                                                                            key[depth]=keys[num[depth]][i];
                                          /* Place i. char of key reprensentation of num at pos depth to to key */
                                                                                            if (depth < strlen (num)+1) {
                                          /* Not finished. Go one position further */                                                 make_key (num,key,depth+1);
                                                                                            } else {
                                            /* finished. Terminate the key and print it */                                                          key[depth+1]=0;
                                                                                        printf ("%s\n",key);
                                                                                            }
                                                                                        }

if you enter 2 the output should be
A
B
C
if you enter 23 the output should be
AD
AE
AF
BD
BE
BF
CD
CE
CF
0
rbrCommented:
#include <stdio.h>
#include <string.h>

void make_key (char *num, char *key, int depth);                                    
char *keys[10]={"","","ABC","DEF","GHI","JKL","MNO","PRS","TUV","WXY"};

int main (int argn, char *argv[])
{
   char num[10];
   char key[10];
   int number;

   printf ("Enter number ?");
   scanf ("%d",&number);
   sprintf (num,"%d",number);                                  
   make_keys(num,key,0);                                
   return (1);                                
}                                  
void make_key (char *num, char *key, int depth)
{                                    
   int i;                                    
   for (i=0;i<strlen(keys[num[depth]]);i++) {                                    
        key[depth]=keys[num[depth]][i];
        if (depth < strlen (num)+1) {                                      
             make_key (num,key,depth+1);                                        
        } else {                                          
             key[depth+1]=0;                                    
             printf ("%s\n",key);
        }                                    
   }                                    
}              

Copy and paste didnt work, sorry.                    
0
LushousAuthor Commented:
Unfortunately Ive tried and tried but i cant get this to work! I have ran in and basically i get it to "enter number" it then allows me to enter the number but then terminates!!!!!
I am running it/editing etc in the Microsoft Visual C++ software.

Do you know what the cause of this is?? Also, could you explain to me why you do some things?? I have only really done the basics in C before and I havnt learbt some of the code below and was wondering if you could explain to me.I have put comments in capitals where i dont understand -

#include <stdio.h>
#include <string.h>

void make_key (char *num, char *key, int depth);                                      
char *keys[10]={"","","ABC","DEF","GHI","JKL","MNO","PRS","TUV","WXY"};

int main (int argn, char *argv[]) //WHY DO YOU USE INT MAIN THEN THOSE ARGUMENTS??WHAT DOES IT DO? HOW COME WE DONT USE THE NORMAL PLAIN MAIN??
{
   char num[10];
   char key[10];
   int number;

   printf ("Enter number ?");
   scanf ("%d",&number);
   sprintf (num,"%d",number);  /*WHY SPRINTF HERE                                
 
make_keys(num,key,0);                                  
return (1); /*WHY DO YOU USE THE (1) HERE?? WHAT DOES IT DO?                                
}
                                   
void make_key (char *num, char *key, int depth)
{                                      
   int i;                                    
   for (i=0;i<strlen(keys[num[depth]]);i++) {                                      
        key[depth]=keys[num[depth]][i];
        if (depth < strlen (num)+1) {                                        
             make_key (num,key,depth+1);                                          
        } else {                                            
             key[depth+1]=0;                                    
             printf ("%s\n",key);
        }                                      
   }                                      
}              

0
_lychee_Commented:
rbr,

pardon me if u'm wrong, but there seems to be a slight prob... i haven't run the code but anyway, shouldn't u minus ascii for '0' b4 referencing keys? cos num is a char array that contains eg. "12345"?
0
rbrCommented:
Thx lychee. Yes that is the error.
for (i=0;i<strlen(keys[num[depth]]);i++) {
should be
for (i=0;i<strlen(keys[num[depth]-`0`]);i++) {
and
 key[depth]=keys[num[depth]][i];
should be
 key[depth]=keys[num[depth]-`0`][i];
0
rbrCommented:
I wrote the code first for integers and after this I change it to characters.
0
rbrCommented:
return (1)

main is a int procedure. So you should return somethink to the OS. Is good for error checking.
0
rbrCommented:
int argn;
char *argv[];
argn is are the numbers od argumets passed to the function + 1 since the program name itself is one argument
*argv[] is a pointer field where these arguments are stored.
Read the C-Faqs at
http://www.eskimo.com/~scs/C-faq/top.html
0
LushousAuthor Commented:
Thanks rbr, but i now have errors I have never come across before??? What do i do??? This is how I now have the program...

#include <stdio.h>
#include <string.h>                  

// Function Declarations
void make_key (char *num, char *key, int depth); // num string of the number you want to convert key current key generated  depth                                    
char *keys[10]={"","","ABC","DEF","GHI","JKL","MNO","PRS","TUV","WXY"}; //initalisation of the keys

int main (int argn, char *argv[])
{
   char num[10];
   char key[10];
   int number;

   instructions (); //Displays instructions

   printf (" enter number ?");
   scanf ("%d",&number);
   
   sprintf (num,"%d",number);                                  
   make_key(num,key,0);
   
   return (1);                                  
}  

                               

//Function for Make Key ()
void make_key (char *num, char *key, int depth)
{                                      
   int i;                                    
   for (i=0;i<strlen(keys[num[depth]-`0`]);i++) {  
   
         key[depth]=keys[num[depth]-`0`][i];                                
       
       
            if (depth < strlen (num)+1) {                                        
             
                  make_key (num,key,depth+1);                                          
       
            } else {                                            
           
                  key[depth+1]=0;                                    
            printf ("%s\n",key);
        }                                      
   }                                      
}              

0
LushousAuthor Commented:
The errors that come up now are -2x   "unknown character 0x60"
2x "<: signed/unsigned mismatch"
0
rbrCommented:
check
the ´ ´ in the 2 lines where . If you have done copy and paste the could be coneverted through the net.
You can use 48 instead of ´0´ too.
in which line does the signed/usigned error occur?

btw: Do you know anythink about C. These errors are very basic.
0
rbrCommented:
Maybe you have a very strong error checking. Then change the line
for (i=0;i<strlen(keys[num[depth]-`0`]);i++)
to
for (i=0;i<(int)strlen(keys[num[depth]-`0`]);i++)
and
if (depth < strlen (num)+1) {    
to
if (depth <(int)( strlen (num)+1)) {    
0
LushousAuthor Commented:
I dont know a tremendous lot unfortunately. Im still trying to pick things up and learn.

Thank for the above, the only thing is that nothing prints out......????

Im so sorry.
0
rbrCommented:
Oh sorry
if (depth <(int)( strlen (num)+1)) {  
should be
if (depth <(int)( strlen (num)-1)) {  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LushousAuthor Commented:
Thank you very much for all your help with this program! I appreciate it!!!!

Thank you for all your patience with me too.I hope one day i can return the favour.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.