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
- برمجة سكالا
- 2021-09-24
- mhanasmh00489829403
الأجوبة
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
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال
معلومات ذات صلة