Reverse a string preserving space positions | C # program to implement the above approach ; Initialize two pointers as two corners ; Move both pointers toward each other ; If character at start or end is space , ignore it ; If both are not spaces , do swap ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void preserveSpace ( string str ) { int n = str . Length ; int start = 0 ; int end = n - 1 ; char [ ] Str = str . ToCharArray ( ) ; while ( start < end ) { if ( Str [ start ] == ' ▁ ' ) { start ++ ; continue ; } else if ( Str [ end ] == ' ▁ ' ) { end -- ; continue ; } else { char temp = Str [ start ] ; Str [ start ] = Str [ end ] ; Str [ end ] = temp ; start ++ ; end -- ; } } Console . Write ( new string ( Str ) ) ; } static void Main ( ) { string str = " internship ▁ at ▁ geeks ▁ for ▁ geeks " ; preserveSpace ( str ) ; } }