#C1549. Sentence Reversal with Punctuation Preservation
Sentence Reversal with Punctuation Preservation
Sentence Reversal with Punctuation Preservation
Given a sentence, your task is to reverse the order of its words while preserving the positions of all punctuation marks and extra spaces. A word is defined as a sequence of alphanumeric characters (matching the regex \b\w+\b
). Non-alphanumeric characters such as punctuation and spaces should remain in their original positions.
For example, if the input is "Hello, world!", then the output should be "world, Hello!".
Note: The reversal is performed only on the words while leaving punctuation and spacing untouched.
inputFormat
The input consists of a single line which contains the sentence to be processed.
You can assume that the sentence may include letters, digits, punctuation marks, and spaces.
outputFormat
Output a single line: the sentence with the order of words reversed while preserving the positions of spaces and punctuation marks.
## sampleHello, world!
world, Hello!