Q:

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

0

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.

All Answers

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

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

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