#C13062. Reverse Words While Preserving Positions
Reverse Words While Preserving Positions
Reverse Words While Preserving Positions
You are given a string that may contain multiple words separated by spaces. Your task is to reverse each word within the string individually while keeping the order of the words unchanged.
For example, if the input is hello world
, the output should be olleh dlrow
.
Mathematically, if a word is represented as \(w = c_1c_2\ldots c_n\), you are required to compute its reverse \(w' = c_nc_{n-1}\ldots c_1\). The overall output string is the concatenation of these reversed words, preserving the spacing between them.
Handle edge cases like empty strings properly.
inputFormat
The input consists of a single line read from standard input (stdin
) containing a string. The string may include multiple words separated by spaces, or it may be empty.
outputFormat
The output should be printed to standard output (stdout
). It should be a single line containing the words from the input string, where each word has been reversed, but the order of the words remains the same.
hello world
olleh dlrow