From fb169134bf1a5ef03b9aeb258f130c3b0a6b6f3a Mon Sep 17 00:00:00 2001 From: David Phillips Date: Thu, 5 Jan 2017 22:50:17 +1300 Subject: Add string permutation tool --- .gitignore | 1 + words-misc/permutations.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 words-misc/permutations.c diff --git a/.gitignore b/.gitignore index 4db000d..00e3b3e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ words-misc/match_pool +words-misc/permutations monty/monty *.o 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 +#include +#include + +/* 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 \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]); + } +} -- cgit v1.1