# Left Rotation and Right Rotation of a String

Given a string of size n, write functions to perform following operations on string.

1. Left (Or anticlockwise) rotate the given string by d elements (where d <= n)
2. Right (Or clockwise) rotate the given string by d elements (where d <= n).

Examples:

```Input : s = "GeeksforGeeks"
d = 2
Output : Left Rotation  : "eksforGeeksGe"
Right Rotation : "ksGeeksforGee"

Input : s = "qwertyu"
d = 2
Output : Left rotation : "ertyuqw"
Right rotation : "yuqwert"
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A Simple Solution is to use a temporary string to do rotations. For left rotation, first copy last n-d characters, then copy first d characters in order to the temporary string. For right rotation, first copy last d characters, then copy n-d characters.

Can we do both rotations in-place and O(n) time?
The idea is based on reversal algorithm for rotation.

```// Left rotate string s by d (Assuming d <= n)
leftRotate(s, d)
reverse(s, 0, d-1); // Reverse substring s[0..d-1]
reverse(s, d, n-1); // Reverse substring s[d..n-1]
reverse(s, 0, n-1); // Reverse whole string.

// Right rotate string s by d (Assuming d <= n)
rightRotate(s, d)

// We can also call above reverse steps
// with d = n-d.
leftRotate(s, n-d)
```

Below is the implementation of above steps :

## C++

 `// 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 = ``"GeeksforGeeks"``; ` `    ``leftrotate(str1, 2); ` `    ``cout << str1 << endl; ` ` `  `    ``string str2 = ``"GeeksforGeeks"``; ` `    ``rightrotate(str2, 2); ` `    ``cout << str2 << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program for Left Rotation and Right  ` `// Rotation of a String  ` `import` `java.util.*; ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `         `  `    ``// function that rotates s towards left by d  ` `    ``static` `String leftrotate(String str, ``int` `d) ` `    ``{ ` `            ``String ans = str.substring(d) + str.substring(``0``, d); ` `            ``return` `ans; ` `    ``} ` ` `  `    ``// function that rotates s towards right by d  ` `    ``static` `String rightrotate(String str, ``int` `d) ` `    ``{ ` `            ``return` `leftrotate(str, str.length() - d); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `            ``String str1 = ``"GeeksforGeeks"``;  ` `            ``System.out.println(leftrotate(str1, ``2``)); ` ` `  `            ``String str2 = ``"GeeksforGeeks"``;  ` `            ``System.out.println(rightrotate(str2, ``2``));  ` `    ``} ` `} ` ` `  `// This code is contributed by rachana soma `

## C#

 `// C# program for Left Rotation and Right  ` `// Rotation of a String  ` `using` `System;  ` `         `  `class` `GFG  ` `{ ` `         `  `    ``// function that rotates s towards left by d  ` `    ``static` `String leftrotate(String str, ``int` `d) ` `    ``{ ` `            ``String ans = str.Substring(d,str.Length-d) + str.Substring(0, d); ` `            ``return` `ans; ` `    ``} ` ` `  `    ``// function that rotates s towards right by d  ` `    ``static` `String rightrotate(String str, ``int` `d) ` `    ``{ ` `            ``return` `leftrotate(str, str.Length - d); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String []args) ` `    ``{ ` `            ``String str1 = ``"GeeksforGeeks"``;  ` `            ``Console.WriteLine(leftrotate(str1, 2)); ` ` `  `            ``String str2 = ``"GeeksforGeeks"``;  ` `            ``Console.WriteLine(rightrotate(str2, 2));  ` `    ``} ` `} ` ` `  `/* This code is contributed by PrinciRaj1992 */`

Output:

```Left rotation:  eksforGeeksGe
Right rotation:  ksGeeksforGee
```

This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

11

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.