fork download
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4.   GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
  5.   C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
  6.   Code, Compile, Run and Debug online from anywhere in world.
  7.  
  8. *******************************************************************************/
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. // Function to swap two characters
  13. void swap(char *x, char *y) {
  14. printf("Character swap %c and %c\n",*x,*y);
  15. char temp;
  16. temp = *x;
  17. *x = *y;
  18. *y = temp;
  19. }
  20.  
  21. // Function to generate all combinations of a string
  22. void generateCombinations(char *str, int start, int end) {
  23. printf("Step 1*****\n");
  24. if (start == end) {
  25. printf("Step 2*****\n");
  26. printf("%s\n", str);
  27. } else {
  28. printf("Step 3*****\n");
  29. for (int i = start; i <= end; i++) {
  30. printf("Step 4***** i=%d\n",i);
  31. swap((str + start), (str + i));
  32. printf("Step 5***** i=%d\n",i);
  33. generateCombinations(str, start + 1, end);
  34. printf("Step 6***** i=%d\n",i);
  35. printf("swap After %s\n",str);
  36. swap((str + start), (str + i)); // backtrack
  37. printf("Step 7***** i=%d\n",i);
  38. }
  39. }
  40. }
  41.  
  42. int main() {
  43. char str[] = "ABCD";
  44. int n = strlen(str);
  45. generateCombinations(str, 0, n - 1);
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0.01s 5284KB
stdin
45
stdout
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