Leonardo's Apprentice
Solutions A simple solution that makes use of extra space consists in simply copying words backwards to a temporary array starting from the last one.
However, an classical in-place solution exists and works in two steps: first reverse the string blindly and then reverse every single word individually.
Note that the input of this puzzle contains multiple lines and empty ones must be preserved.
Here is a C++ solution:
1 2 3 4 5 6 7 8 9 10 11 12 std::string s{std::istreambuf_iterator<char>(std::cin), {}}; // read full input into memory, preserving newlines and spaces std::reverse(begin(s), end(s)); // reverse the string blindly auto head = begin(s); while (head !
[Read More]