fork download
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. void combination(string a, int l, int r)
  6. {
  7. if (l == r) // Base case
  8. cout<<a<<endl;
  9.  
  10. else
  11. {
  12. for (int i = l; i <= r; i++) // Permutations made
  13. {
  14. swap(a[l], a[i]); // Swapping done
  15.  
  16. combination(a, l+1, r); // Recursion called
  17.  
  18. swap(a[l], a[i]); //backtrack
  19. }
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. string str = "ABC";
  26. int n = str.size();
  27. combination(str, 0, n-1);
  28. return 0;
  29. }
  30.  
  31.  
Success #stdin #stdout 0s 4200KB
stdin
Standard input is empty
stdout
ABC
ACB
BAC
BCA
CBA
CAB