[OP hasn't shown up on news.qwest.net yet]:
| In article <
[email protected]>,
|| I'm trying to learn how to make a scrambling algorithm in C that
|| will turn out all possible permutations of a word without
|| repeating (but repeating can easily be fixed). I found out a few
|| algorithms in comp.lang.c++, but none of them work.
Recursion is fun. I came up with the following; but you'll need to
clean it up a bit because I didn't check the return value from
malloc() and left a debug printf() in place so you can better see
what's going on during execution.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void permute(char *fixed,char *remain)
{ unsigned i,len = strlen(fixed);
char *newrem;
unsigned char c;
if (strlen(remain))
{ newrem = malloc(1+strlen(remain));
fixed[len+1] = '\0';
for (i=0; i<strlen(remain); i++)
{ fixed[len] = remain
;
strcpy(newrem,remain);
strcpy(newrem+i,remain+i+1);
printf("permute(\"%s\",\"%s\")\n",fixed,newrem);
permute(fixed,newrem);
}
fixed[len] = '\0';
}
else puts(fixed);
}
int main(void)
{ char left[16]="\0",right[16]="ABCDE";
permute(left,right);
return 0;
}
Hope this helps to get you started (I'm sure there's a better way.)