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

  • برمجة سكالا

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

الأجوبة

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

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

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

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