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

  • برمجة سكالا

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

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

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

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