#include // write #include // malloc, free int ft_strlen(const char *s) { int len = 0; while (s[len]) len++; return (len); } void print_solution(const char *s) { write(1, s, ft_strlen(s)); write(1, "\n", 1); } void ft_swap(char *a, char *b) { char temp = *a; *a = *b; *b = temp; } void sort_string(char *str, int n) { int i; int j; i = 0; while (i < n - 1) { j = 0; while (j < n - i - 1) { if (str[j] > str[j + 1]) ft_swap(&str[j], &str[j + 1]); j++; } i++; } } void generate_permutations(char *original_str, char *current_perm, int *used, int n, int k) { if (k == n) { print_solution(current_perm); return; } int i = 0; while (i < n) { if (used[i] == 0) { current_perm[k] = original_str[i]; used[i] = 1; generate_permutations(original_str, current_perm, used, n, k + 1); used[i] = 0; } i++; } } int main(int argc, char **argv) { if (argc != 2 || !argv[1][0]) return (0); int n = ft_strlen(argv[1]); char *original_str = argv[1]; sort_string(original_str, n); char *current_perm = (char *)malloc(sizeof(char) * (n + 1)); if (!current_perm) return (1); current_perm[n] = '\0'; int *used = (int *)calloc(sizeof(int) * n); if (!used) { free(current_perm); return (1); } generate_permutations(original_str, current_perm, used, n, 0); free(current_perm); free(used); return (0); }