Traversing string in reverse order
for ( int i = n - 1 ; i >= 0 ; i -- ) if ( str [ i ] == ' a ' ) { str2 = str2 . Substring ( 0 , i ) + ' b ' + str2 . Substring ( i + 1 ) ; p ++ ; if ( p == k ) break ; } } Console . Write ( str2 ) ; }