diff options
Diffstat (limited to 'words-misc')
-rw-r--r-- | words-misc/permutations.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/words-misc/permutations.c b/words-misc/permutations.c new file mode 100644 index 0000000..4fafde0 --- /dev/null +++ b/words-misc/permutations.c @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* FIXME hacked together 6 months ago, not sure if finished */ +/* FIXME does duplicates */ + +void swap_chr(char*, char*); +void p(char *, size_t); + +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: %s <word>\n", argv[0]); + return EXIT_FAILURE; + } + + p(argv[1], 0); + + return 0; +} + +void swap_chr(char *a, char *b) { + char t = *a; + *a = *b; + *b = t; +} + +void p(char *string, size_t x) +{ + size_t i = 0; + + if (x == strlen(string)) { + puts(string); + return; + } + for (i = 0; i < strlen(string); i++) { + swap_chr(&string[x], &string[i]); + p(string, x + 1); + swap_chr(&string[x], &string[i]); + } +} |