-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathPermutations.c
More file actions
31 lines (25 loc) · 789 Bytes
/
Permutations.c
File metadata and controls
31 lines (25 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <string.h>
void permutations(char p[], char up[]) {
// Base case: If the remaining characters to be permuted is empty, print the permutation
if (up[0] == '\0') {
printf("%s\n", p);
return;
}
char ch = up[0];
int len = strlen(p);
for (int i = 0; i <= len; i++) {
// Split the permutation string into two parts
char f[100], s[100];
strncpy(f, p, i); // First part
f[i] = '\0';
strcpy(s, p + i); // Second part
// Recursive call: Generate permutations by inserting the current character at each possible position
permutations(strcat(strcat(f, &ch), s), up + 1);
}
}
int main() {
char str[] = "abc";
permutations("", str);
return 0;
}