Scala Programming: Check two given strings whether any one of them appear at the end of the other string
Write a Scala program to check two given strings whether any one of them appear at the end of the other string (ignore case sensitivity).
Sample Solution:
Scala Code:
object Scala_String {
def test(str1: String, str2: String): Boolean = {
var stng1 = str1.toLowerCase();
var aLen = str1.length;
var stng2 = str2.toLowerCase;
var bLen = stng2.length();
if (aLen < bLen) {
var temp = stng2.substring(bLen - aLen, bLen);
if (temp.compareTo(stng1) == 0)
true;
else
false;
} else {
var temp = stng1.substring(aLen - bLen, aLen);
if (temp.compareTo(stng2) == 0)
true;
else
false;
}
}
def main(args: Array[String]): Unit = {
var str1 = "pqrxyz";
var str2 = "xyz";
println("The given strings are: " + str1 + " and " + str2);
println("Is one string appears at the end of other? " + test(str1, str2));
str1 = "pqrxyz";
str2 = "rxy";
println("The given strings are: " + str1 + " and " + str2);
println("Is one string appears at the end of other? " + test(str1, str2));
}
}
Sample Output:
The given strings are: pqrxyz and xyz Is one string appears at the end of other? true The given strings are: pqrxyz and rxy Is one string appears at the end of other? false
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to read a string and returns after remove a specified character and its immediate left and right characters.
Next: Write a Scala program to check whether a substring appears before a period(.) within a given string.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics