Q:

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

0

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

All Answers

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

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

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