Q:

Write a Scala program to read two strings append them together and return the result

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now