Reverse Words
EasyPractice library · +10 XPstringsloopssimple parsing
Given a single line of text containing words separated by single spaces, reverse each word individually while preserving the order of the words and the spaces between them. A word is defined as a sequence of non-space characters.
Input
A single line of text.
Output
The modified line of text with each word reversed.
Constraints
The line contains at most 1000 characters. Words consist of alphanumeric characters. There are no leading/trailing spaces and words are separated by exactly one space.
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
hello world
Output
olleh dlrow
Each word is reversed independently.
Example 2
Input
a b c
Output
a b c
Single character words remain unchanged after reversal.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
hello world
Example 2 input
a b c