Scala Programming: 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.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, make sure you are familiar with the following concepts:
- Strings in Scala: How to work with strings and perform operations like counting characters within them.
- Counting Occurrences: The count method in Scala allows you to count the occurrences of a specific character in a string.
- Conditional Statements: Using if or logical operators to check if a value lies within a specific range.
- Return Types: The function should return a Boolean indicating whether the condition is met or not.
Hints (Try Before Looking at the Solution!)
Consider the following hints to guide you through solving this exercise:
- Hint 1: Define a function that takes a string as input.
- Hint 2: Use the count method to count how many times the character 'z' appears in the string.
- Hint 3: Check if the count of 'z' falls within the range 2 to 4 (inclusive) using logical AND.
- Hint 4: The function should return true if the count is within the range; otherwise, it should return false.
- Hint 5: Test the function with various strings to verify that it works correctly.
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?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics