Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char* del_elem(const char list[], int len, int pos)
- {
- char* temp = malloc((len - 1) * sizeof(char));
- memcpy(temp, list, pos * sizeof(char));
- memcpy(&temp[pos], &list[pos+1], len - pos);
- return temp;
- }
- void perm(char current_string[], int current_position, const char alphabet[], int len)
- {
- if(len != 0)
- for(int i = 0; i < len; i++)
- {
- current_string[current_position] = alphabet[i];
- perm(current_string, current_position + 1, del_elem(alphabet, len, i), len - 1);
- }
- else
- printf("%s\n", current_string);
- }
- void print_permutation(const char alphabet[], int len)
- {
- char* temp = calloc(len, sizeof(char));
- perm(temp, 0, alphabet, len);
- }
- int main(void)
- {
- char alphabet[] = {'a', 'b', 'c', 'd'};
- print_permutation(alphabet, sizeof(alphabet) / sizeof(alphabet[0]));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment