MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / reverse

Function reverse

Exercises/Modules/Chapter 08/Soln8_02.cpp:30–40  ·  view source on GitHub ↗

Reverse a string in place The code here is working with a copy of the argument so the original is not affected.

Source from the content-addressed store, hash-verified

28// The code here is working with a copy of the argument
29// so the original is not affected.
30std::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}

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected