c - All possible combinations of a word -


i in job interview , asked me generate list of possible permutations of given string. solution inefficient , guy interview me told me supposed use recursion. know question?

this classic interview question, solution goes that:

int permu(char* str, size_t len ,size_t index ) {        size_t = index - 1;      if(index == len) { printf ("%s\n",str); }       while (++i < len)     {          swap (str,index,i);           /* swap between index , */         permu(str, len ,index + 1 );  /* recorsion */         swap (str,index,i);           /* swap between index , */     }      return(0); } 

note, in code user should give 0 in index parmether better call function this:

int permutations(char* str, size_t len) {     return (permu(str, len ,0)); }  static int permu(char* str, size_t len ,size_t index ) { //....} 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -