{"id":17324,"date":"2021-08-26T10:42:42","date_gmt":"2021-08-26T05:12:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=17324"},"modified":"2021-11-22T18:37:11","modified_gmt":"2021-11-22T13:07:11","slug":"python-program-to-determine-whether-one-string-is-a-rotation-of-another","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-determine-whether-one-string-is-a-rotation-of-another\/","title":{"rendered":"Python Program to Determine Whether one String is a Rotation of Another"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Shuffle a List<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to determine whether one string is the rotation of another.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Shuffle a List Given a string, and the task is to determine whether one string is a rotation of another. Examples: Example1: Input: Given first string = “btechgeeks” Given second string = “geeksbtech” Output: The given second string is the rotation of the given second …<\/p>\n
\nGiven a string, and the task is to determine whether one string is a rotation of another.<\/p>\nGiven first string = \"btechgeeks\"\r\nGiven second string = \"geeksbtech\"<\/pre>\n
The given second string is the rotation of the given second string<\/pre>\n
Given first string = \"pqrst\"\r\nGiven second string = \"sggsf\"<\/pre>\n
The given second string is not the rotation of the given second string<\/pre>\n
Program to Determine Whether one String is a Rotation of Another<\/h2>\n
\n
Method #1: Using String Concatenation (Static Input)<\/h3>\n
\n
# Give the first string as static input and store it in a variable.\r\nfst_str = \"pqrst\"\r\n# Give the second string as static input and store it in another variable.\r\nsecnd_str = \"stpqr\"\r\n# Check if the length of the first string is not equal to the length of the second\r\n# string using the if conditional statement and using the len() function.\r\nif(len(fst_str) != len(secnd_str)):\r\n # If the statement is true, print \"The given second string is not the rotation of the given first string\".\r\n print(\"The given second string is not the rotation of the given first string\")\r\nelse:\r\n # Else concatenate the first string with the first string itself using the '+ ' operator\r\n # and store it in a variable say \"conat_str\".\r\n conct_str = fst_str + fst_str\r\n# Check if the second string is present in the \"conca_str\" using the if\r\n# conditional statement.\r\n if(secnd_str in conct_str):\r\n # If the statement is true, print\u00a0 \"The given second string is the rotation of the given first string\".\r\n print(\"The given second string is the rotation of the given\u00a0 first string\")\r\n else:\r\n # Else print\u00a0 \"The given second string is not the rotation of the given first\u00a0string\".\r\n print(\"The given second string is not the rotation of the first given string\")\r\n<\/pre>\n
The given second string is the rotation of the second string<\/pre>\n
Method #2: Using String Concatenation (User Input)<\/h3>\n
\n
# Give the first string as user input using input() function and store it in a variable.\r\nfst_str = input(\"Enter some random string = \")\r\n# Give the second string as user input using input() function and store it in another variable.\r\nsecnd_str = input(\"Enter some random string = \")\r\n# Check if the length of the first string is not equal to the length of the second\r\n# string using the if conditional statement and using the len() function.\r\nif(len(fst_str) != len(secnd_str)):\r\n # If the statement is true, print \"The given second string is not the rotation of the given first string\".\r\n print(\"The given second string is not the rotation of the given\u00a0first\u00a0string\")\r\nelse:\r\n # Else concat the first string with the first string itself using the '+ ' operator\r\n # and store it in a variable say \"conat_str\".\r\n conct_str = fst_str + fst_str\r\n# Check if the second string is present in the \"conca_str\" using the if\r\n# conditional statement.\r\n if(secnd_str in conct_str):\r\n # If the statement is true, print\u00a0 \"The given second string is the rotation of the given\u00a0first string\".\r\n print(\"The given second string is the rotation of the given\u00a0first\u00a0string\")\r\n else:\r\n # Else print\u00a0 \"The given second string is not the rotation of the given\u00a0first string\".\r\n print(\"The given second string is not the rotation of the given\u00a0first\u00a0string\")\r\n<\/pre>\n
Enter some random string = btechgeeks\r\nEnter some random string = geeksbtech\r\nThe given second string is the rotation of the second string<\/pre>\n
\n