Reverse individual words with O ( 1 ) extra space | C ++ implementation of the approach ; Function to resturn the string after reversing the individual words ; Pointer to the first character of the first word ; If the current word has ended ; Pointer to the last character of the current word ; Reverse the current word ; Pointer to the first character of the next word ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string reverseWords ( string str ) { int start = 0 ; for ( int i = 0 ; i <= str . size ( ) ; i ++ ) { if ( str [ i ] == ' ▁ ' || i == str . size ( ) ) { int end = i - 1 ; while ( start < end ) { swap ( str [ start ] , str [ end ] ) ; start ++ ; end -- ; } start = i + 1 ; } } return str ; } int main ( ) { string str = " Geeks ▁ for ▁ Geeks " ; cout << reverseWords ( str ) ; return 0 ; }