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

  • برمجة سكالا

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.

الأجوبة

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
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...