Replace all occurrences of string AB with C without using extra space | Efficient C # program to replace all occurrences of " AB " with " C " ; Index in modified string ; Index in original string ; Traverse string ; Replace occurrence of " AB " with " C " ; Increment j by 2 ; add a null character to terminate string ; Driver code
using System ; class GFG { static void translate ( char [ ] str ) { int len = str . Length ; if ( len < 2 ) return ; int i = 0 ; int j = 0 ; while ( j < len - 1 ) { if ( str [ j ] == ' A ' && str [ j + 1 ] == ' B ' ) { j = j + 2 ; str [ i ++ ] = ' C ' ; continue ; } str [ i ++ ] = str [ j ++ ] ; } if ( j == len - 1 ) str [ i ++ ] = str [ j ] ; str [ i ] = ' ▁ ' ; str [ len - 1 ] = ' ▁ ' ; } public static void Main ( ) { String st = " helloABworldABGfG " ; char [ ] str = st . ToCharArray ( ) ; translate ( str ) ; Console . Write ( " The ▁ modified ▁ string ▁ is ▁ : " ) ; Console . Write ( str ) ; } }