Q:

Write a Scala program to make a new string from two given string in such a way that, each character of two string will come respectively

0

Write a Scala program to make a new string from two given string in such a way that, each character of two string will come respectively.

All Answers

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

object Scala_String {
  def test(stng1: String, stng2: String): String = {
    val len1 = stng1.length;
    val len2 = stng2.length;
    var max_len = Math.max(len1, len2);
    var newstring = "";
    for (i <- 0 to max_len - 1) {
      if (i <= len1 - 1)
        newstring = newstring + stng1.substring(i, i + 1);
      if (i <= len2 - 1)
        newstring = newstring + stng2.substring(i, i + 1);
    }
    newstring;
  }

  def main(args: Array[String]): Unit = {
    val str1 = "welcome";
    val str2 = "w3resource";
    println("The given strings  are: " + str1 + "  and  " + str2);
    println("The new string is: " + test(str1, str2));
  }
}

Sample Output:

The given strings  are: welcome  and  w3resource
The new string is: wwe3lrceosmoeurce

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