c - Finding all possible words from inputted phone number -


this question has answer here:

i have problem need in figuring out. hoping few pointers on better way approach i'm doing. main issue few lines below (//this whats hanging me up) , described @ bottom of page.

i need permutate possible outcomes of phone number: (not dictionary words)

i.e. 222-2222

should output list 3^7 long possible permutations of a,b,c

i.e.

aaaaaaa aaaaaab aaaaaac aaaaaba   // whats hanging me aaaaabb aaaaabc aaaaaca   // here , on 

my code (purposely shortened testing) gives me:

aaaa aaab aaac aabc aacc abcc accc bccc cccc 

i'm beginning programming student knowledge goes far using for, while, if, statements , grabbing individual chars array.

here's code looks far: (this part of function. code missing)

char alphafunc(char n[]){

int d1=n[0]-48; int d2=n[1]-48; int d3=n[2]-48; int d4=n[3]-48; int d5=n[4]-48; int d6=n[5]-48; int d7=n[6]-48; int a=0,b=0,c=0,d=0,e=0,f=0,g=0;  int i=0;  char chararray[10][4]={ {'0','0','0'},{'1','1','1'},{'a','b','c'},         {'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},         {'p','r','s'},{'t','u','v'},{'w','x','y'}  };    while(i <=14){      printf("%c%c%c%c\n", chararray[d1][a],             chararray[d2][b],chararray[d3][c],chararray[d4][d],             chararray[d5][e],chararray[d6][f],chararray[d7][g]);     g++;      if(g==3){         g=2;         f++;     }     if(f==3){         f=2;         e++;     }     if(e==3){         e=2;         d++;     } 

i'm not looking me need little in figuring out sort of statement work b/c when have digit chararray[d-][a] location [3] , reset [0] sends different part of loop. (hope makes sense).

since values of chararray constant, recommend making global variable, rather declaring in function. in addition, since numbers have 4 letters, whereas others have 3, may want using jagged array represent it.

as far printing permutations can phone number, think recursion going friend. assuming can store phone number in int array, following should work:

public void printpermutations(int[] phonenumber) {    printpermutations(phonenumber, 0, string.empty); }  private void printpermutations(int[] phonenumber, int index, string permutation) {    if(index >= phonenumber.length)    {        // if we've reached end, print number        printf(permutation + "\n");    }    else    {        // otherwise, generate permutation each        // character digit can        int digit = phonenumber[index];        char[] chars = chararray[digit];        (int = 0; < chars.length; i++)        {            printpermutations(phonenumber, index+1, permutation + chars[i]);        }    } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -