text: Find the direction from given string | Function to find the final direction ; if count is positive that implies resultant is clockwise direction ; if count is negative that implies resultant is anti - clockwise direction ; Driver code
code: < ? php function findDirection ( $ s ) { $ count = 0 ; $ d = " " ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ 0 ] == ' ' ) return null ; if ( $ s [ $ i ] == ' L ' ) $ count -= 1 ; else { if ( $ s [ $ i ] == ' R ' ) $ count += 1 ; } } if ( $ count > 0 ) { if ( $ count % 4 == 0 ) $ d = " N " ; else if ( $ count % 4 == 1 ) $ d = " E " ; else if ( $ count % 4 == 2 ) $ d = " S " ; else if ( $ count % 4 == 3 ) $ d = " W " ; } if ( $ count < 0 ) { if ( $ count % 4 == 0 ) $ d = " N " ; else if ( $ count % 4 == -1 ) $ d = " W " ; else if ( $ count % 4 == -2 ) $ d = " S " ; else if ( $ count % 4 == -3 ) $ d = " E " ; } return $ d ; } $ s = " LLRLRRL " ; echo findDirection ( $ s ) . " STRNEWLINE " ; $ s = " LL " ; echo findDirection ( $ s ) . " STRNEWLINE " ; ? >