C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to determine whether one string is a rotation of anotherExplanationIn 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
SolutionPythonstr1 = "abcde"; str2 = "deabc"; if(len(str1) != len(str2)): print("Second string is not a rotation of first string"); else: try: #Concatenate str1 with str1 and store it in str1 str1 = str1 + str1; #Check whether str2 is present in str1 if(str1.index(str2)): print("Second string is a rotation of first string"); except ValueError: print("Second string is not a rotation of first string"); Output: Second string is a rotation of first string C#include <stdio.h> #include <string.h> int main() { char str1[] = "abcde", str2[] = "deabc"; if(strlen(str1) != strlen(str2)){ printf("Second string is not a rotation of first string"); } else{ //Concatenate str1 with str1 and store it in str1 strcat(str1, str1); //Check whether str2 is present in str1 if(strstr(str1, str2) != NULL) printf("Second string is a rotation of first string"); else printf("Second string is not a rotation of first string"); } return 0; } Output: Second string is a rotation of first string JAVApublic class StringRotation { public static void main(String[] args) { String str1 = "abcde", str2 = "deabc"; if(str1.length() != str2.length()){ System.out.println("Second string is not a rotation of first string"); } else { //Concatenate str1 with str1 and store it in str1 str1 = str1.concat(str1); //Check whether str2 is present in str1 if(str1.indexOf(str2) != -1) System.out.println("Second string is a rotation of first string"); else System.out.println("Second string is not a rotation of first string"); } } } Output: Second string is a rotation of first string C#using System; public class StringRotation { public static void Main() { String str1 = "abcde", str2 = "deabc"; if(str1.Length != str2.Length){ Console.WriteLine("Second string is not a rotation of first string"); } else { //Concatenate str1 with str1 and store it in str1 str1 = String.Concat(str1, str1); //Check whether str2 is present in str1 if(str1.IndexOf(str2) != -1) Console.WriteLine("Second string is a rotation of first string"); else Console.WriteLine("Second string is not a rotation of first string"); } } } Output: Second string is a rotation of first string PHP<!DOCTYPE html> <html> <body> <?php $str1 = "abcde"; $str2 = "deabc"; if(strlen($str1) != strlen($str2)){ print("Second string is not a rotation of first string"); } else { //Concatenate str1 with str1 and store it in str1 $str1 = $str1.$str1; //Check whether str2 is present in str1 if(strpos($str1, $str2) != false) print("Second string is a rotation of first string"); else print("Second string is not a rotation of first string"); } ?> </body> </html> Output: Second string is a rotation of first string
Next Topic#
|