Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions rotation_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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()
{
string str1 = "anupam and arnab";
leftrotate(str1, 2);
cout << str1 << endl;

string str2 = "anupam and arnab";
rightrotate(str2, 2);
cout << str2 << endl;
return 0;
}