#K60022. Reverse Each Word
Reverse Each Word
Reverse Each Word
Given a sentence, reverse the characters of each word individually while preserving the order of the words and the exact spacing. In other words, for each contiguous sequence of non-space characters (i.e. a word), reverse its letters, but leave all space characters in their original positions.
For example, given the input Hello World
, the output should be olleH dlroW
. Similarly, if the input contains multiple spaces, such as Hello World
, the spaces must be preserved in the output: olleH dlroW
.
If \(S\) is the input string, then the transformation can be formally defined as follows:
\[ \text{output}(S)_i = \begin{cases} S_{i'} & \text{if } S_i \text{ belongs to a word and } i' \text{ is its reversed index in the word},\\ S_i & \text{if } S_i \text{ is a space.} \end{cases} \]Note that if the input is empty, the output should also be empty.
inputFormat
The input consists of a single line containing the sentence \(S\) to be transformed. The sentence may contain leading, trailing, or multiple intermediate spaces.
outputFormat
Output the transformed sentence where each word's characters are reversed while preserving the original spacing. Print the result to standard output.
## sampleHello World
olleH dlroW