Write a Scala program to create a new string where 'if' is added to the front of a given string. If the string already begins with 'if', return the string unchanged

  • برمجة سكالا

Write a Scala program to create a new string where 'if' is added to the front of a given string. If the string already begins with 'if', return the string unchanged.

الأجوبة

object scala_basic {
  def test(str: String): String = 
    {
     if (str.startsWith("if")) str else "if " + str
    }
     
   def main(args: Array[String]): Unit = {
      println("Result: " + test("if else"));
      println("Result: " + test("else"));
    }
}

Sample Output:

Result: if else
Result: if else
هل كان المحتوى مفيد؟

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

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

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