/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <stdio.h> #include <string.h> // Function to swap two characters void swap(char *x, char *y) { printf("Character swap %c and %c\n",*x,*y); char temp; temp = *x; *x = *y; *y = temp; } // Function to generate all combinations of a string void generateCombinations(char *str, int start, int end) { printf("Step 1*****\n"); if (start == end) { printf("Step 2*****\n"); printf("%s\n", str); } else { printf("Step 3*****\n"); for (int i = start; i <= end; i++) { printf("Step 4***** i=%d\n",i); swap((str + start), (str + i)); printf("Step 5***** i=%d\n",i); generateCombinations(str, start + 1, end); printf("Step 6***** i=%d\n",i); printf("swap After %s\n",str); swap((str + start), (str + i)); // backtrack printf("Step 7***** i=%d\n",i); } } } int main() { char str[] = "ABCD"; int n = strlen(str); generateCombinations(str, 0, n - 1); return 0; }
45
Step 1***** Step 3***** Step 4***** i=0 Character swap A and A Step 5***** i=0 Step 1***** Step 3***** Step 4***** i=1 Character swap B and B Step 5***** i=1 Step 1***** Step 3***** Step 4***** i=2 Character swap C and C Step 5***** i=2 Step 1***** Step 2***** ABCD Step 6***** i=2 swap After ABCD Character swap C and C Step 7***** i=2 Step 4***** i=3 Character swap C and D Step 5***** i=3 Step 1***** Step 2***** ABDC Step 6***** i=3 swap After ABDC Character swap D and C Step 7***** i=3 Step 6***** i=1 swap After ABCD Character swap B and B Step 7***** i=1 Step 4***** i=2 Character swap B and C Step 5***** i=2 Step 1***** Step 3***** Step 4***** i=2 Character swap B and B Step 5***** i=2 Step 1***** Step 2***** ACBD Step 6***** i=2 swap After ACBD Character swap B and B Step 7***** i=2 Step 4***** i=3 Character swap B and D Step 5***** i=3 Step 1***** Step 2***** ACDB Step 6***** i=3 swap After ACDB Character swap D and B Step 7***** i=3 Step 6***** i=2 swap After ACBD Character swap C and B Step 7***** i=2 Step 4***** i=3 Character swap B and D Step 5***** i=3 Step 1***** Step 3***** Step 4***** i=2 Character swap C and C Step 5***** i=2 Step 1***** Step 2***** ADCB Step 6***** i=2 swap After ADCB Character swap C and C Step 7***** i=2 Step 4***** i=3 Character swap C and B Step 5***** i=3 Step 1***** Step 2***** ADBC Step 6***** i=3 swap After ADBC Character swap B and C Step 7***** i=3 Step 6***** i=3 swap After ADCB Character swap D and B Step 7***** i=3 Step 6***** i=0 swap After ABCD Character swap A and A Step 7***** i=0 Step 4***** i=1 Character swap A and B Step 5***** i=1 Step 1***** Step 3***** Step 4***** i=1 Character swap A and A Step 5***** i=1 Step 1***** Step 3***** Step 4***** i=2 Character swap C and C Step 5***** i=2 Step 1***** Step 2***** BACD Step 6***** i=2 swap After BACD Character swap C and C Step 7***** i=2 Step 4***** i=3 Character swap C and D Step 5***** i=3 Step 1***** Step 2***** BADC Step 6***** i=3 swap After BADC Character swap D and C Step 7***** i=3 Step 6***** i=1 swap After BACD Character swap A and A Step 7***** i=1 Step 4***** i=2 Character swap A and C Step 5***** i=2 Step 1***** Step 3***** Step 4***** i=2 Character swap A and A Step 5***** i=2 Step 1***** Step 2***** BCAD Step 6***** i=2 swap After BCAD Character swap A and A Step 7***** i=2 Step 4***** i=3 Character swap A and D Step 5***** i=3 Step 1***** Step 2***** BCDA Step 6***** i=3 swap After BCDA Character swap D and A Step 7***** i=3 Step 6***** i=2 swap After BCAD Character swap C and A Step 7***** i=2 Step 4***** i=3 Character swap A and D Step 5***** i=3 Step 1***** Step 3***** Step 4***** i=2 Character swap C and C Step 5***** i=2 Step 1***** Step 2***** BDCA Step 6***** i=2 swap After BDCA Character swap C and C Step 7***** i=2 Step 4***** i=3 Character swap C and A Step 5***** i=3 Step 1***** Step 2***** BDAC Step 6***** i=3 swap After BDAC Character swap A and C Step 7***** i=3 Step 6***** i=3 swap After BDCA Character swap D and A Step 7***** i=3 Step 6***** i=1 swap After BACD Character swap B and A Step 7***** i=1 Step 4***** i=2 Character swap A and C Step 5***** i=2 Step 1***** Step 3***** Step 4***** i=1 Character swap B and B Step 5***** i=1 Step 1***** Step 3***** Step 4***** i=2 Character swap A and A Step 5***** i=2 Step 1***** Step 2***** CBAD Step 6***** i=2 swap After CBAD Character swap A and A Step 7***** i=2 Step 4***** i=3 Character swap A and D Step 5***** i=3 Step 1***** Step 2***** CBDA Step 6***** i=3 swap After CBDA Character swap D and A Step 7***** i=3 Step 6***** i=1 swap After CBAD Character swap B and B Step 7***** i=1 Step 4***** i=2 Character swap B and A Step 5***** i=2 Step 1***** Step 3***** Step 4***** i=2 Character swap B and B Step 5***** i=2 Step 1***** Step 2***** CABD Step 6***** i=2 swap After CABD Character swap B and B Step 7***** i=2 Step 4***** i=3 Character swap B and D Step 5***** i=3 Step 1***** Step 2***** CADB Step 6***** i=3 swap After CADB Character swap D and B Step 7***** i=3 Step 6***** i=2 swap After CABD Character swap A and B Step 7***** i=2 Step 4***** i=3 Character swap B and D Step 5***** i=3 Step 1***** Step 3***** Step 4***** i=2 Character swap A and A Step 5***** i=2 Step 1***** Step 2***** CDAB Step 6***** i=2 swap After CDAB Character swap A and A Step 7***** i=2 Step 4***** i=3 Character swap A and B Step 5***** i=3 Step 1***** Step 2***** CDBA Step 6***** i=3 swap After CDBA Character swap B and A Step 7***** i=3 Step 6***** i=3 swap After CDAB Character swap D and B Step 7***** i=3 Step 6***** i=2 swap After CBAD Character swap C and A Step 7***** i=2 Step 4***** i=3 Character swap A and D Step 5***** i=3 Step 1***** Step 3***** Step 4***** i=1 Character swap B and B Step 5***** i=1 Step 1***** Step 3***** Step 4***** i=2 Character swap C and C Step 5***** i=2 Step 1***** Step 2***** DBCA Step 6***** i=2 swap After DBCA Character swap C and C Step 7***** i=2 Step 4***** i=3 Character swap C and A Step 5***** i=3 Step 1***** Step 2***** DBAC Step 6***** i=3 swap After DBAC Character swap A and C Step 7***** i=3 Step 6***** i=1 swap After DBCA Character swap B and B Step 7***** i=1 Step 4***** i=2 Character swap B and C Step 5***** i=2 Step 1***** Step 3***** Step 4***** i=2 Character swap B and B Step 5***** i=2 Step 1***** Step 2***** DCBA Step 6***** i=2 swap After DCBA Character swap B and B Step 7***** i=2 Step 4***** i=3 Character swap B and A Step 5***** i=3 Step 1***** Step 2***** DCAB Step 6***** i=3 swap After DCAB Character swap A and B Step 7***** i=3 Step 6***** i=2 swap After DCBA Character swap C and B Step 7***** i=2 Step 4***** i=3 Character swap B and A Step 5***** i=3 Step 1***** Step 3***** Step 4***** i=2 Character swap C and C Step 5***** i=2 Step 1***** Step 2***** DACB Step 6***** i=2 swap After DACB Character swap C and C Step 7***** i=2 Step 4***** i=3 Character swap C and B Step 5***** i=3 Step 1***** Step 2***** DABC Step 6***** i=3 swap After DABC Character swap B and C Step 7***** i=3 Step 6***** i=3 swap After DACB Character swap A and B Step 7***** i=3 Step 6***** i=3 swap After DBCA Character swap D and A Step 7***** i=3