fork download
  1. #include <fstream>
  2. #include <sstream>
  3. #include <iostream>
  4.  
  5. void print_backwards(std::istream& in)
  6. {
  7. char ch;
  8. if (!in.get(ch)) // base condition: Nothing more to be extracted from stream.
  9. return;
  10.  
  11. print_backwards(in); // extract more input and print it prior to
  12. std::cout << ch; // printing this character.
  13. }
  14.  
  15. int main()
  16. {
  17. // our makeshift file contents. Replace with std::ifstream in("filename")
  18. std::istringstream in(
  19. "Jack and Jill went up the hill\n"
  20. "To fetch a pail of water\n"
  21. "Jack fell down and broke his crown\n"
  22. "And Jill came tumbling after"
  23. );
  24.  
  25. print_backwards(in);
  26. }
  27.  
  28.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
retfa gnilbmut emac lliJ dnA
nworc sih ekorb dna nwod llef kcaJ
retaw fo liap a hctef oT
llih eht pu tnew lliJ dna kcaJ