Q:

Write a Scala program to check whether the first two characters present at the end of a given string

0

Write a Scala program to check whether the first two characters present at the end of a given string.

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): Boolean = {
   if (str1.length < 2)
    return false;
  else if (str1.substring(0,2).equals(str1.substring(str1.length-2, str1.length)))
    return true;
  else
    return false;
  }

  def main(args: Array[String]): Unit = {
      var str1 =  "educated";	  
      println("The given strings is: "+str1);
      println("If first two characters appear in the last! "+test(str1));
      str1 =  "ABCDEFBA";	  
      println("The given strings is: "+str1);
      println("If first two characters appear in the last! "+test(str1));
  }
}

Sample Output:

The given strings is: educated
If first two characters appear in the last! true
The given strings is: ABCDEFBA
If first two characters appear in the 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