Evaluate a boolean expression represented as string | 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
< ? php function evaluateBoolExpr ( $ s ) { $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i += 2 ) { if ( ( $ i + 1 ) < $ n && $ 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 ( ( $ i + 2 ) < $ n && $ s [ $ i + 2 ] == $ s [ $ i ] ) $ s [ $ i + 2 ] = '0' ; else $ s [ $ i + 2 ] = '1' ; } } return $ s [ $ n - 1 ] - '0' ; } $ s = "1C1B1B0A0" ; echo evaluateBoolExpr ( $ s ) ;