Modify a string by performing given shift operations | C ++ implementataion of above approach ; Function to find the string obtained after performing given shift operations ; If shift [ i ] [ 0 ] = 0 , then left shift Otherwise , right shift ; Stores length of the string ; Effective shift calculation ; Stores modified string ; Right rotation ; Left rotation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void stringShift ( string s , vector < vector < int > > & shift ) { int val = 0 ; for ( int i = 0 ; i < shift . size ( ) ; ++ i ) val += shift [ i ] [ 0 ] == 0 ? - shift [ i ] [ 1 ] : shift [ i ] [ 1 ] ; int len = s . length ( ) ; val = val % len ; string result = " " ; if ( val > 0 ) result = s . substr ( len - val , val ) + s . substr ( 0 , len - val ) ; else result = s . substr ( - val , len + val ) + s . substr ( 0 , - val ) ; cout << result ; } int main ( ) { string s = " abc " ; vector < vector < int > > shift = { { 0 , 1 } , { 1 , 2 } } ; stringShift ( s , shift ) ; return 0 ; }