Check if a string can be obtained by rotating another string 2 places | C # program to check if a string is two time rotation of another string . ; Method to check if string2 is obtained by string 1 ; Initialize string as anti - clockwise rotation ; Initialize string as clock wise rotation ; check if any of them is equal to string1 ; Driver code
using System ; public class Test { public static bool isRotated ( string str1 , string str2 ) { if ( str1 . Length != str2 . Length ) { return false ; } if ( str1 . Length < 2 ) { return str1 . Equals ( str2 ) ; } string clock_rot = " " ; string anticlock_rot = " " ; int len = str2 . Length ; anticlock_rot = anticlock_rot + str2 . Substring ( len - 2 , len - ( len - 2 ) ) + str2 . Substring ( 0 , len - 2 ) ; clock_rot = clock_rot + str2 . Substring ( 2 ) + str2 . Substring ( 0 , 2 ) ; return ( str1 . Equals ( clock_rot ) || str1 . Equals ( anticlock_rot ) ) ; } public static void Main ( string [ ] args ) { string str1 = " geeks " ; string str2 = " eksge " ; Console . WriteLine ( isRotated ( str1 , str2 ) ? " Yes " : " No " ) ; } }