I have the code to produce all permutations of a character string.
the code is as follows:
Code:
void RecursivePermute(char str[], int k) {
int j;
// Base-case: Since all letters are fixed, we can ONLY print
// what's stored in str.
if (k == strlen(str))
{
printf("\nBase case: %s\n", str);
}
else {
// Loop through each possible starting letter for index k,
// the first index for which we have a choice.
for (j=k; j<strlen(str); j++) {
// Place the character stored in index j in location k.
ExchangeCharacters(str, k, j);
// Print out all of the permutations with that character
// just chosen above fixed.
RecursivePermute(str, k+1);
// Put the original character that used to be there back
// in its place.
ExchangeCharacters(str, j, k);
}
}
}
This only works if all of the letters in str are different. I don't even know where to begin to prevent "doubles" from printing out...any suggestions?
Thanks