object Scala_String {
def checkForRotation(str1: String, str2: String): Boolean = {
return (str1.length == str2.length) && ((str1 + str1).indexOf(str2) != -1);
}
def main(args: Array[String]): Unit = {
val str1 = "ABACD";
val str2 = "CDABA";
println("The given strings are: " + str1 + " and " + str2);
println("\nThe concatination of 1st string twice is: " + str1 + str1
);
if (checkForRotation(str1, str2)) {
println("The 2nd string " + str2 + " exists in the new string."
);
println("\nStrings are rotations of each other");
}
else {
println("The 2nd string " + str2 + " not exists in the new string.");
printf("\nStrings are not rotations of each other");
}
}
}
Sample Output:
The given strings are: ABACD and CDABA
The concatination of 1st string twice is: ABACDABACD
The 2nd string CDABA exists in the new string.
Strings are rotations of each other
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer