w3resource

Scala Programming: Check whether a given character presents in a string between 2 to 4 times

Scala Programming Basic Exercise-23 with Solution

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

Sample Solution:

Scala Code:

// Define an object named scala_basic
object scala_basic {
  // Define a function named test with a parameter str1 of type String, returning a Boolean
  def test(str1: String): Boolean = {
    // Count the occurrences of the character 'z' in the string and assign it to the variable count_char
    val count_char = str1.count { n => n == 'z' }
    // Check if the count of 'z' is between 2 and 4 (inclusive)
    count_char >= 2 && count_char <= 4
  }
     
  // Define the main method, which is the entry point of the program
  def main(args: Array[String]): Unit = {
    // Print the result of calling test with the argument "frizz"
    println("Result: " + test("frizz"))
    
    // Print the result of calling test with the argument "zane"
    println("Result: " + test("zane"))
    
    // Print the result of calling test with the argument "Zazz"
    println("Result: " + test("Zazz"))
    
    // Print the result of calling test with the argument "false"
    println("Result: " + test("false"))
  }
}

Sample Output:

Result: true
Result: false
Result: true
Result: false

Explanation:

Here is the break down of the said Scala code:

  • object scala_basic {: This declares an object named scala_basic.
  • def test(str1: String): Boolean = {: This line defines a function named test that takes a parameter str1 of type String and returns a Boolean.
  • val count_char = str1.count { n => n == 'z' }: This line counts the occurrences of the character 'z' in the input string str1 and assigns the count to the variable count_char.
  • count_char >= 2 && count_char <= 4: This line checks if the count of 'z' is between 2 and 4 (inclusive). If true, the function returns true; otherwise, it returns false.
  • }: Closes the test function.
  • def main(args: Array[String]): Unit = {: This line defines the main method, which is the entry point of the program. It takes an array of strings (args) as its parameter and returns Unit (similar to void in other languages).
  • println("Result: " + test("frizz")): This line calls the "test()" function with the argument "frizz" and prints the result to the console.
  • println("Result: " + test("zane")): Another call to the "test()" function with the argument "zane".
  • println("Result: " + test("Zazz")): Another call to the "test()" function with the argument "Zazz".
  • println("Result: " + test("false")): Another call to the test function with the argument "false".

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Find the larger value from two positive integer values in the range 20..30 inclusive, or return 0 if neither is in that range.
Next: Check whether two given positive integers have the same last digit.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/scala-exercises/basic/scala-basic-exercise-23.php