![]() |
VOOZH | about |
Given a string s, find a way to reverse the order of the words in the given string.
Note: string may contain leading or trailing dots(.) or multiple trailing dots(.) between two words. The returned string should only have a single dot(.) separating the words.
Examples:
Input: s = "i.like.this.program.very.much"
Output: much.very.program.this.like.i
Explanation: The words in the input string are reversed while maintaining the dots as separators, resulting in"much.very.program.this.like.i".Input: s = ”..geeks..for.geeks.”
Output: geeks.for.geeksInput: s = "...home......"
Output: home
Table of Content
Push all words separated by dots into a stack, then pop each word one by one and append it back to form the reversed string.
geeks.for.geeks
Reverse the entire string, then iterate through it to extract words separated by dots. Reverse each word individually and update the original string until the end is reached. Refer to the figure to understand better...
geeks.for.geeks
We can efficiently solve this problem using built-in library functions like
splitto break the string into words,reverseto reverse the order of words, andstringstream(or equivalent functions) to reconstruct the final reversed string. This approach simplifies implementation and improves readability.
geeks.for.geeks