Write a Scala program to read two strings append them together and return the result. If the length of the strings is different remove characters from the beginning of longer string and make them equal length.
object Scala_String { def test(str1: String, str2: String): String = { if (str1.length == str2.length) return str1 + str2; if (str1.length > str2.length) { var diff = str1.length - str2.length; str1.substring(diff, str1.length) + str2; } else { var diff = str2.length - str1.length; str1 + str2.substring(diff, str2.length); } } def main(args: Array[String]): Unit = { var str1 = "Welcome"; var str2 = "home"; println("The given strings is: " + str1 + " and " + str2); println("The new string is: " + test(str1, str2)); str1 = "Scala"; str2 = "Python"; println("The given strings is: " + str1 + " and " + str2); println("The new string is: " + test(str1, str2)); } }
Sample Output:
The given strings is: Welcome and home The new string is: comehome The given strings is: Scala and Python The new string is: Scalaython
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output: