![]() |
VOOZH | about |
Given string str, we need to print the reverse of individual words.
Examples:
Input: Hello World
Output: olleH dlroW
Explanation: Each word in "Hello World" is reversed individually, preserving the original order, resulting in "olleH dlroW".Input: Geeks for Geeks
Output: skeeG rof skeeG
We use a stack to store words character by character until we encounter a space. When a space is found, we pop and print the characters from the stack, effectively reversing the word. This process continues for all words in the string
skeeG rof skeeG
To reverse individual words in a string, we can use built-in functions like
stringstreamin C++, StringBuilder in Java,splitin Python and other languages. After splitting the string into words, we iterate over each word and reverse it using thereversefunction.
skeeG rof skeeG