-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperm.c
More file actions
39 lines (35 loc) · 665 Bytes
/
perm.c
File metadata and controls
39 lines (35 loc) · 665 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
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <string.h>
#include <assert.h>
void swap(char * s, int i, int j){
char temp = s[i];
s[i] = s[j];
s[j] = temp;
return;
}
void helper(char* s, int i){
if (s[i] == '\0'){
printf("%s\n", s);
}else{
int l = strlen(s);
for (int j = i; j < l; j++){
swap(s, i, j);
helper(s, i+1);
swap(s, i, j);
}
}
return;
}
void generate_permutations(const char* input_string){
assert(strlen(input_string) < 50);
char s[50];
strcpy(s, input_string);
helper(s, 0);
return;
}
int main(int argc, char *argv[]){
if (argc > 1){
generate_permutations(argv[1]);
}
return 1;
}