Return previous element in an expanding matrix | C # program to return previous element in an expanding matrix ; Returns left of str in an expanding matrix of a , b , c and d . ; Start from rightmost position ; If the current character is b or d , change to a or c respectively and break the loop ; If the current character is a or c , change it to b or d respectively ; driver program to test above method
using System ; using System . Text ; public class GFG { public static StringBuilder findLeft ( StringBuilder str ) { int n = str . Length ; while ( n > 0 ) { n -- ; if ( str [ n ] == ' d ' ) { str [ n ] = ' c ' ; break ; } if ( str [ n ] == ' b ' ) { str [ n ] = ' a ' ; break ; } if ( str [ n ] == ' a ' ) { str [ n ] = ' b ' ; } else if ( str [ n ] == ' c ' ) { str [ n ] = ' d ' ; } } return str ; } public static void Main ( string [ ] args ) { StringBuilder str = new StringBuilder ( " aacbddc " ) ; Console . Write ( " Left ▁ of ▁ " + str + " ▁ is ▁ " + findLeft ( str ) ) ; } }