Reverse a string in place The code here is working with a copy of the argument so the original is not affected.
| 28 | // The code here is working with a copy of the argument |
| 29 | // so the original is not affected. |
| 30 | std::string reverse(std::string str) |
| 31 | { |
| 32 | const size_t length {str.length()}; |
| 33 | for (size_t i {}; i < length / 2; ++i) |
| 34 | { |
| 35 | char temp = str[i]; |
| 36 | str[i] = str[length - i - 1]; |
| 37 | str[length - i - 1] = temp; |
| 38 | } |
| 39 | return str; |
| 40 | } |