Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. //7.31
  2. #include <iostream>
  3. #include <string>
  4. void stringReverse(const std::string&, int start = -1);
  5. int main(int argc, const char* argv[]) {
  6.     std::cout << "Program to recursively print a string backwards" << std::endl;
  7.     std::string string1 = "This is a string1";
  8.     std::string string2 = "String 2 looks like this";
  9.     std::cout << string1 << std::endl;
  10.     stringReverse(string1, string1.length());
  11.     std::cout << std::endl;
  12.     std::cout << string2 << std::endl;
  13.     stringReverse(string2);
  14.     return 0;
  15. }
  16. // recursively prints a string in reverse
  17. void stringReverse(const std::string& st, int start) {
  18.     // account for lack of start index
  19.     if (start == -1) { start = st.length(); }
  20.     std::cout << st[start];
  21.     // base case
  22.     if (start == 0) {
  23.         std::cout << std::endl;
  24.         return;
  25.     }
  26.     stringReverse(st, --start);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement