Here is a standard function to print the permutations of characters of a string:
void permute(char *a, int i, int n)
{
int j;
if (i == n)
printf(\
Take notes which chars you swapped previously:
char was[256];
/*
for(j = 0; j <= 255; j++)
was[j] = 0;
*/
bzero(was, 256);
for (j = i; j <= n; j++)
{
if (!was[*(a+j)]) {
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j)); //backtrack
was[*(a+j)] = 1;
}
}
This has to be the fastest one from the entries so far, some benchmark on a "AAAABBBCCD" (100 loops):
native C - real 0m0.547s
STL next_permutation - real 0m2.141s