Write a Scala program to check whether a given character presents in a string between 2 to 4 times

  • برمجة سكالا

Write a Scala program to check whether a given character presents in a string between 2 to 4 times.

الأجوبة

object scala_basic {
  def test(str1: String): Boolean = {
      val count_char = str1.count{ n => n == 'z' }
      count_char >= 2 && count_char <= 4
    }
     
   def main(args: Array[String]): Unit = {
      println("Result: " + test("frizz"));
      println("Result: " + test("zane"));
      println("Result: " + test("Zazz"));
      println("Result: " + test("false"));
    }
  }

Sample Output:

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

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

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

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