• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
July 22, 2022 |4.8K Views
C++ Program to Generate all Rotations of a given String
Description
Discussion

In this video, we will see C++ Program to generate all rotations of a given string.

Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. 

So, in given string S we will print all the possible rotated strings.

Examples: 

Input : S = "geeks"
Output : 
geeks
eeksg
eksge
ksgee
sgeek

Input : S = "abc" 
Output : 
abc
bca
cab

Here we will see three different approaches for the same:
1. Using the Iterative method(for loop)
2. Using Concatenate string method
3. Using substr()

The idea is to run a loop from i = 0 to n – 1, i.e for each point of rotation, copy the second part of the string in the temporary string and then copy the first part of the original string to the temporary string.

In the next method, we do str. str where . is the concatenation operator. Now we traverse the concatenated string from 0 to n – 1 and print all substrings of size n.

In another method, we use the string Concat method to check all rotations of the string.

Program to generate rotations of given string:
https://www.geeksforgeeks.org/generate-rotations-given-string/