Q:

Write a Scala program to read a string and return true if it ends with a specified string of length 2

0

Write a Scala program to read a string and return true if it ends with a specified string of length 2.

All Answers

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

object Scala_String {
  def test(str1: String, end_str: String): Boolean = {
    val len = str1.length
    val ng = end_str
    if (len < 2)
      false
    else if (ng.equals(str1.substring(len - 2, len)))
      true;
    else
      false
  }
  def main(args: Array[String]): Unit = {
    var str1 = "String";
    var end_str = "ng"
    println("The given strings is: " + str1)
    println("The string containing " + end_str + " at last: " + test(str1, end_str))
    end_str = "gn"
    println("The given strings is: " + str1)
    println("The string containing " + end_str + " at last: " + test(str1, end_str))
  }
}

Sample Output:

The given strings is: String
The string containing ng at last: true
The given strings is: String
The string containing gn at last: false

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