Evaluate a boolean expression represented as string | C # program to evaluate value of an expression . ; Evaluates boolean expression and returns the result ; Traverse all operands by jumping a character after every iteration . ; If operator next to current operand is AND . ; If operator next to current operand is OR . ; If operator next to current operand is XOR ( Assuming a valid input ) ; Driver code
using System ; using System . Text ; class GFG { public static int evaluateBoolExpr ( StringBuilder s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i += 2 ) { if ( i + 1 < n && i + 2 < n ) { if ( s [ i + 1 ] == ' A ' ) { if ( s [ i + 2 ] == '0' s [ i ] == 0 ) { s [ i + 2 ] = '0' ; } else { s [ i + 2 ] = '1' ; } } else if ( ( i + 1 ) < n && s [ i + 1 ] == ' B ' ) { if ( s [ i + 2 ] == '1' s [ i ] == '1' ) { s [ i + 2 ] = '1' ; } else { s [ i + 2 ] = '0' ; } } else { if ( s [ i + 2 ] == s [ i ] ) { s [ i + 2 ] = '0' ; } else { s [ i + 2 ] = '1' ; } } } } return s [ n - 1 ] - '0' ; } public static void Main ( string [ ] args ) { string s = "1C1B1B0A0" ; StringBuilder sb = new StringBuilder ( s ) ; Console . WriteLine ( evaluateBoolExpr ( sb ) ) ; } }