Your code seems correct, but it's just C. In C ++, using the same approach, it might look something like this:
#include <iostream> #include <string> #include <iterator> #include <algorithm> #include <cctype> int main() { std::string str = " cat cow dog wolf lobster"; std::string result; auto it = str.begin(); while (it != str.end()) { while (it != str.end() && isspace(*it)) { result += *it; ++it; } auto begin = it; while (it != str.end() && !isspace(*it)) { ++it; } auto end = it; result.append(std::reverse_iterator<decltype(end)>(end), std::reverse_iterator<decltype(begin)>(begin)); // if you want to modify original string instead, just do this: std::reverse(begin, end); } std::cout << result <<'\n'; }
source share