Link to home
Start Free TrialLog in
Avatar of Lushous
Lushous

asked on

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.







Avatar of arnond
arnond

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.
Avatar of Lushous

ASKER

Edited text of question.
Avatar of Lushous

ASKER

Edited text of question.
do you have any algorithm for the conversion ? any written stuff on how to convert ?

Arnon David.
Avatar of Lushous

ASKER

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.
Sorry I still not understand the connection between 2 and A B C.
What should be the output for 3?
Avatar of Lushous

ASKER

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.
i don't understad it either. please give a few more examples for input\output.

Arnon David.
Avatar of Lushous

ASKER

*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.
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);
          }
      }
}
To start use

char key[10];

make_key ("26082",key,0);
Avatar of Lushous

ASKER

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!
Avatar of Lushous

ASKER

The keys should be -
2   ABC
3   DEF
4   GHI
5   JKL
6   MNO
7   PRS
8   TUV
9   WXY


                                              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);
                                                  }
                                              }
Avatar of Lushous

ASKER

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??
Avatar of Lushous

ASKER

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!


Avatar of Lushous

ASKER

I have been playing wioth this program all day but cant get it to work!!!!!!!
Avatar of Lushous

ASKER

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?????

Avatar of Lushous

ASKER

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.
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
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
#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.                    
Avatar of Lushous

ASKER

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);
        }                                      
   }                                      
}              

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"?
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];
I wrote the code first for integers and after this I change it to characters.
return (1)

main is a int procedure. So you should return somethink to the OS. Is good for error checking.
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
Avatar of Lushous

ASKER

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);
        }                                      
   }                                      
}              

Avatar of Lushous

ASKER

The errors that come up now are -2x   "unknown character 0x60"
2x "<: signed/unsigned mismatch"
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.
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)) {    
Avatar of Lushous

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of rbr
rbr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lushous

ASKER

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.