/
AllPermOfString.c
113 lines (104 loc) · 1.86 KB
/
AllPermOfString.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//To print all possible permutations of string
#include<stdio.h>
#include<string.h>
#include<math.h>
//Integer to binary representation
void toBinary(int i,int n,char * str)
{
int j,k,temp,pos=0;
temp=i;
pos=n-1;
while(temp!=0)
{
//str[pos--]=(temp%2==1)?49:48;
if(temp==1)
{
str[pos]=49;
break;
}
if(temp%2==0)
str[pos--]=49;
else
str[pos--]=48;
temp/=2;
}
}
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
/* Function to print permutations of string
This function takes three parameters:
1. String
2. Starting index of the string
3. Ending index of the string. */
void permute(char *a, int i, int n)
{
int j;
if (i == n && a[0]!=0)
printf("%s\n", a);
else
{
for (j = i; j <= n; j++)
{
swap((a + i), (a + j));
permute(a, i + 1, n);
swap((a + i), (a + j)); //backtrack
}
}
}
/*
int main(){
char a[] = { "abcd" };
permute(a, 0, 3);
getchar();
return 0;
}
*/
int main()
{
int i, j, n, num, ctr;
char * str, **all_sets;
char * temp_str;// = (char *)malloc(sizeof(char)*n);
printf("Enter num of chars in string: ");
scanf("%d",&n);
scanf("%c", &j);
num=(int)pow((double)2,(double)n);
printf("Enter a string: ");
str=(char *) malloc(sizeof(char)*(n+1));
temp_str = (char *)malloc(sizeof(char)*(n+1));
str[n]=0;
for(i=0;i<n;i++)
{
scanf("%c",&str[i]);
}
all_sets=(char **) malloc(sizeof(char *) * num);
for(i=0;i<num;i++)
{
all_sets[i]=(char *) malloc(sizeof(char)*(n+1));
memset(all_sets[i],48,n);
all_sets[i][n]=0;
toBinary(i,n,all_sets[i]);
}
for(i=0;i<num;i++)
{
ctr = 0;
memset(temp_str, 0, n+1);
//printf("%s\n",all_sets[i]);
for(j=0;j<n;j++)
{
if (all_sets[i][n - j - 1] == 49){
temp_str[ctr++] = str[j];
}
//printf("%c",str[j]);
}
if (temp_str[0] != 0){
permute(temp_str, 0, ctr-1);
//printf("%s\n", temp_str);
}
}
return 0;
}