/* 
 This is a tool to generate all combinations
 with a set of characters. 

 to compile : gcc a_ccode.c -o a_name
 to use     : ./a_name
              ./a_name > filename.txt
              to write all the combinations 
              to filename.txt


 linux example :
 [root@localhost ... ]# gcc a_ccode.c -o anagr
 [root@localhost ... ]# ./anagr
 Input : abc
 bca
 cba
 cab
 acb
 bac
 abc
 [root@localhost ... ]# 

 pseudo-code :

 permute (index, word)
 begin
      for i=1 to index
      begin 
              swap    (word[i], word[index])
              permute (index-1, word)
              swap    (word[i], word[index])
      end
      if index = 1 then print woord
 end

 c-code:
*/

#include <stdio.h>
#include <string.h>

void permute (int index, char *word) {
 int i;
 char c;
 if(--index) {
  for(i=0; i<=index; i++) {
   c=word[i]; word[i]=word[index]; word[index]=c; /* swap */
   permute(index,word);
   c=word[i]; word[i]=word[index]; word[index]=c; /* swap */
  };
 }
 else { 
  printf("%s\n",word);
 }; 
};

int main () {
 char c,a[255];
 int i;
     
 printf("Input : ");
 for (i = 0;(c = getchar()) != '\n'; ++i) {
  a[i] = c;
  if (isalpha(c)); 
 }
 a[i] = '\0';
 
 permute(strlen(a),a);
 return(0);
};

