fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void subs(string sub, string ans) {
  6. // Base case: if the string is empty, print the answer
  7. if (sub.length() == 0) {
  8. cout << ans << endl;
  9. return;
  10. }
  11.  
  12. char ch = sub[0]; // Extract the first character
  13. string ros = sub.substr(1); // The Rest Of String (ros)
  14.  
  15. // Recursive call 1: Do not include the character in the answer
  16. subs(ros, ans);
  17.  
  18. // Recursive call 2: Include the character in the answer
  19. subs(ros, ans + ch);
  20. }
  21.  
  22. int main() {
  23. string s = "abc";
  24.  
  25. // Call the function directly
  26. subs(s, "");
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
c
b
bc
a
ac
ab
abc