code.cpp 357 B

123456789101112131415
  1. /* Create a function that takes a string as its argument
  2. ~ and returns the string in reversed order.
  3. */
  4. std::string reverse(std::string str) {
  5. std::string s = "";
  6. // get length of "str"
  7. int len = str.size();
  8. // loop over "str" to reverse it
  9. for (int i=0; i < len; i++) {
  10. s += str.back();
  11. // str = last + str;
  12. str.pop_back();
  13. }
  14. return s;
  15. }