In doing this, we could
- use a more high-level C++ approach,
std::string reverse(const std::string &str); // ... std::string reversed = reverse("uncategorized"); printf(reversed); // dezirogetacnuwhere the function returns a reversed copy of the original string. (Btw, to reverse a std::string, use the std::reverse algorithm, see bottom of this post.)
Alternatively, we can
- write a C-styled function, using a "raw" pointer and then modify the string, in-place:
void reverseInPlace(char *str); // ... char s[] = "november"; reverseInPlace(s); printf(s); // rebmevonSince we are going for minimal memory footprint and optimal performance, we will pursue the latter approach.