What about simply passing the minimum and maximum length into the function, and printing the string when start
is between the two?
Code:
void permute(char *str, int start, int n, int minLength, int maxLength)
{
int i;
if (start >= minLength)
{
char temp = str[start]; // store the character, so we don't lose it
str[start] = 0; // 0x00 - end of string
printf("%s\n", str);
str[start] = temp;
}
if (start == maxLength)
return;
for (i = start; i < n; i++)
{
swap(str+i, str+start);
permute(str, start+1, n, minLength, maxLength);
swap(str+i, str+start);
}
}
If there are duplicates in the data, and you want to prevent duplicate permutations, all you need to do is:
Sort the letters to start so any repeated characters are next to each other
Don't do anything if the last character was the same as this one. This can be done by simply adding the following code to the beginning of the for-loop:
if (i != start && str[i] == str[i-1]) continue;