Add Dramatic Punctuation to Text
Implement add_drama(text). Preserve all whitespace exactly. For each maximal non-whitespace token, first replace every period . in that token with !, then append one additional ! to the token.
Other characters are unchanged. An empty string or a string containing only whitespace is returned unchanged.
Constraints
-
0 <= len(text) <= 10^6
-
Input may contain spaces, tabs, and newlines.
-
The output must be built in linear time.
Examples
-
"hello. world"
becomes
"hello!! world!"
.
-
"a b.c"
becomes
"a! b!c!"
.
-
" "
remains
" "
.
Clarifications
A token that already ends in ! still receives the required extra !. Replacing a final period and then appending therefore produces two exclamation marks.
Hints
Track whether the scan is inside a token so the extra punctuation is emitted exactly at a token boundary.
Extensions
-
Process the text as a character stream.
-
Treat Unicode grapheme clusters as characters.
-
Make the replacement and suffix characters configurable.