Reverse individual words with O ( 1 ) extra space | C # implementation of the approach ; Function to resturn the string after reversing the individual words 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
using System ; class GFG { static String reverseWords ( String str ) { int start = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' ▁ ' i == str . Length - 1 ) { int end ; if ( i == str . Length - 1 ) end = i ; else end = i - 1 ; while ( start < end ) { str = swap ( str , start , end ) ; start ++ ; end -- ; } start = i + 1 ; } } return str ; } static String swap ( String str , int i , int j ) { char [ ] ch = str . ToCharArray ( ) ; char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . Join ( " " , ch ) ; } public static void Main ( String [ ] args ) { String str = " Geeks ▁ for ▁ Geeks " ; Console . WriteLine ( reverseWords ( str ) ) ; } }