From e8f63b8e3ac794007d7a8d6d75e9f65917f760e0 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 1 Nov 2017 16:41:36 +0100 Subject: anagrams in c --- cpp/anagrams.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 cpp/anagrams.c (limited to 'cpp/anagrams.c') diff --git a/cpp/anagrams.c b/cpp/anagrams.c new file mode 100644 index 0000000..018e00e --- /dev/null +++ b/cpp/anagrams.c @@ -0,0 +1,25 @@ +#include +#include +void swap(char* a, char* b) { + char t = *a; + *a = *b; + *b = t; +} + +void anagrams(char* str, int l, int r) { + if(l == r) + printf("%s\n", str); + else + for(int i = l; i < r; i++) { + swap(&str[l], &str[i]); + anagrams(str, l+1, r); + //swap(&str[l], &str[i]); + } +} + +int main() { + char str[30]; + scanf("%s", str); + anagrams(str, 0, strlen(str)); + return 0; +} -- cgit v1.2.3-18-g5258