![]() |
VOOZH | about |
Given a sentence in the form of string str, the task is to reverse each word of the given sentence in C++.
Examples:
Input: str = "the sky is blue"
Output: blue is sky the
Input: str = "I love programming"
Output: programming love I
Below is the implementation of the above approach:
code this like I
Time Complexity: O(n)
Auxiliary Space: O(1)
: We can create the reverse() function which is used to reverse the given string. Below are the steps:
Below is the implementation of the above approach:
code this like I
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3: Without Using Extra Space
The above task can also be accomplished by splitting and directly swapping the string starting from the middle. As direct swapping is involved, less space is consumed too.
practice of lot a needs coding at good getting
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using stringstream and vector
code this like I
Time complexity is O(n)
Auxiliary space is O(n)