Guest User

Untitled

a guest
Dec 11th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char* del_elem(const char list[], int len, int pos)
  6. {
  7.   char* temp = malloc((len - 1) * sizeof(char));
  8.  
  9.   memcpy(temp, list, pos * sizeof(char));
  10.   memcpy(&temp[pos], &list[pos+1], len - pos);
  11.  
  12.   return temp;
  13. }
  14.  
  15. void perm(char current_string[], int current_position, const char alphabet[], int len)
  16. {
  17.   if(len != 0)
  18.     for(int i = 0; i < len; i++)
  19.     {
  20.       current_string[current_position] = alphabet[i];
  21.       perm(current_string, current_position + 1, del_elem(alphabet, len, i), len - 1);
  22.     }
  23.   else
  24.     printf("%s\n", current_string);
  25. }
  26.  
  27. void print_permutation(const char alphabet[], int len)
  28. {
  29.   char* temp = calloc(len, sizeof(char));
  30.  
  31.   perm(temp, 0, alphabet, len);
  32. }
  33.  
  34. int main(void)
  35. {
  36.   char alphabet[] = {'a', 'b', 'c', 'd'};
  37.  
  38.   print_permutation(alphabet, sizeof(alphabet) / sizeof(alphabet[0]));
  39.  
  40.   return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment