summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2017-01-05 22:50:17 +1300
committerDavid Phillips <david@sighup.nz>2017-01-05 22:50:17 +1300
commitfb169134bf1a5ef03b9aeb258f130c3b0a6b6f3a (patch)
tree2cc34d6900011941a3750490b449d47bd537e03b
parent49cea086d392546ea77ac488ba756fa4ddd638c8 (diff)
downloadodds-and-ends-fb169134bf1a5ef03b9aeb258f130c3b0a6b6f3a.tar.xz
Add string permutation tool
-rw-r--r--.gitignore1
-rw-r--r--words-misc/permutations.c43
2 files changed, 44 insertions, 0 deletions
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 <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]);
+ }
+}