Explanation
In this program, we need to check whether a string is a rotation of another string or not.
String 1: abcde
String 2 : deabc
String 1 + String 1: abcdeabcde
Consider above example, suppose we need to check whether string 2 is a rotation of string 1. To find this, we concatenate string 1 with string 1. Then, try to find the string 2 in concatenated string. If string 2 is present in concatenated string then, string 2 is rotation of string 1. String 2 deabc is found on the index 3 in concatenated string. So, deabc is rotation of abcde.
Algorithm
- Define two string 1 and string 2.
- To check whether string 2 is rotation of string 1 then, first check the length of both the strings. If they are not equal, then string 2 cannot be a rotation of string 1.
- Concatenate string 1 with itself and assign it to string 1.
- Check the index of string 2 in string 1. If it exists then, string 2 is a rotation of string 1.
Input:
str1 = "abcde"
Output:
Second string is a rotation of first string.
Python
Output:
C
Output:
JAVA
Output:
C#
Output:
PHP
Output: