in C++ 3.1 Input The first line of input will be an integer N, representing the
ID: 3740548 • Letter: I
Question
in C++
Explanation / Answer
// C program for Left Rotation and Right
// Rotation of a String
#include<bits/stdc++.h>
using namespace std;
// In-place rotates s towards left by d
void leftrotate(string &s, int d)
{
reverse(s.begin(), s.begin()+d);
reverse(s.begin()+d, s.end());
reverse(s.begin(), s.end());
}
// In-place rotates s towards right by d
void rightrotate(string &s, int d)
{
leftrotate(s, s.length()-d);
}
// Driver code
int main()
{
int a,b;
char c;
string str1, str2;
cin>>a;
cin>>b;
cin>>c;
for(int i=0;i<a;i++){
cin >> str1;
if(c=='l'){
leftrotate(str1,b);
str2.append(str1);
str2.append(".");
}
else if(c=='r'){
rightrotate(str1,b);
str2.append(str1);
str2.append(".");
}
}
cout<<str2;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.