Scala Programming: Check whether a given string starts with 'Sc' or not
Write a Scala program to check whether a given string starts with 'Sc' or not.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Scala Objects: Understanding how to define objects using the object keyword.
- Functions in Scala: How to create functions that take parameters and return values.
- String Methods: Using the startsWith() method to check if a string starts with a particular prefix.
- Boolean Values: Understanding how to work with boolean values (true/false).
- Conditionals: Using the if statement to check conditions and return values.
- Main Method: The main() method is the entry point in a Scala program.
- Printing to Console: Using println() to output results to the console.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Define an object to hold your Scala code.
- Hint 2: Write a function that accepts a string as an argument and returns a boolean.
- Hint 3: Use the startsWith() method to check if the string starts with "Sc".
- Hint 4: Test your function with various strings such as "Scala", "abcd", and "sc".
- Hint 5: Return true if the string starts with "Sc", otherwise return false.
Sample Solution:
Scala Code:
// Define an object named scala_basic
object scala_basic {
// Define a function named test with parameter str1 of type String, returning a Boolean
def test(str1: String): Boolean =
{
// Check if the string starts with the prefix "Sc"
str1.startsWith("Sc")
}
// 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 "Scala"
println("Result: " + test("Scala"))
// Print the result of calling test with the argument "abcd"
println("Result: " + test("abcd"))
// Print the result of calling test with the argument "sc"
println("Result: " + test("sc"))
// Print the result of calling test with the argument "a"
println("Result: " + test("a"))
}
}
Sample Output:
Result: true Result: false Result: false 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. The function checks if the string starts with the prefix "Sc" using the startsWith method. If true, it returns true; otherwise, it returns false.
- str1.startsWith("Sc"): This line is the implementation of the "test()" function. It checks if the input string str1 starts with the prefix "Sc" and returns a Boolean value indicating whether the condition is true or false.
- 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("Scala")): This line calls the "test()" function with the argument "Scala", concatenates the result with the string "Result: ", and prints the entire string to the console.
- println("Result: " + test("abcd")): Similar to the previous line, this calls the "test()" function with the argument "abcd".
- println("Result: " + test("sc")): Another call to the "test()" function with the argument "sc".
- println("Result: " + test("a")): Another call to the test function with the argument "a".
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3, use whatever characters are there.
Next: Check whether one of the given temperatures is less than 0 and the other is greater than 100.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics