c - permutations for pass generation -
from original code generates possible combinations of 3 numbers:
void gen(char *word, char *chars, int ug, int length) { size_t = 0; int u = ug; while(i < strlen(chars)){ word[u] = chars[i++]; if(u < (length-1)) { gen(word, chars, ++u, length); u--; } else { printf("%s\n", word); } } } int main(int argc, char* argv[]) { char *chars = "0123456789"; /* 3 char long */ int = 3; int length = 3; /* allocate memory output */ char *word = calloc(length, 0); gen(word, chars, 0, 3); return 0; }
but cause need function work differently, modified this:
char *genpass(char* pass,int len, int crt, size_t i) { char *chars = "0123456789"; pass[crt] = chars[i++]; return pass; } int main(int argc, char* argv[]) { char *pass = calloc(10, 0); int crt;//current permutation int len = 3; size_t = 0; (crt=0;crt<10;crt++) { pass = genpass(pass,len,crt,i); printf("pass: %s\n", pass); i++; //some other code work pass } return 0; }
but returns:
pass: 0 pass: 01 pass: 012 pass: 0123 pass: 01234 pass: 012345 pass: 0123456 pass: 01234567 pass: 012345678 pass: 0123456789
what did mess up? how can make generate correctly first 10 permutations of 3 length numbers?
now function genpass() not recursive!
if want generate 10 permutations, in code when printf , increment counter each time printf permutation, , when counter 10 break while
Comments
Post a Comment