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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer