From bf39baee101d48f05b9a1b3a93dbe97ad7c42e46 Mon Sep 17 00:00:00 2001 From: anupamdas43345 <55242195+anupamdas43345@users.noreply.github.com> Date: Thu, 17 Oct 2019 17:42:56 +0530 Subject: [PATCH] Create rotation_string.cpp --- rotation_string.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 rotation_string.cpp diff --git a/rotation_string.cpp b/rotation_string.cpp new file mode 100644 index 0000000..c87ddc5 --- /dev/null +++ b/rotation_string.cpp @@ -0,0 +1,31 @@ +// C program for Left Rotation and Right +// Rotation of a String +#include +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; +}