text: Find the direction from given string | CPP implementation of above approach ; 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: #include <bits/stdc++.h> NEW_LINE using namespace std ; string findDirection ( string s ) { int count = 0 ; string d = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ 0 ] == ' ' ) return NULL ; if ( s [ i ] == ' L ' ) count -- ; else { if ( s [ i ] == ' R ' ) count ++ ; } } 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 ; } int main ( ) { string s = " LLRLRRL " ; cout << ( findDirection ( s ) ) << endl ; s = " LL " ; cout << ( findDirection ( s ) ) << endl ; }