Scala Programming: Check whether a string 'yt' appears at index 1 in a given string
Write a Scala program to check whether a string 'yt' appears at index 1 in a given string. If it appears return a string without 'yt' otherwise return the original string.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Strings in Scala: Understanding how to work with strings in Scala, including how to access substrings and modify them.
- String Methods: Familiarity with string methods such as startsWith() and replaceFirst() to check string contents and replace parts of a string.
- Conditionals in Scala: Writing if statements to check conditions and control the flow of execution based on those conditions.
- String Manipulation: Using methods like drop() to remove characters and manipulate strings.
- Function Return Values: Defining functions that return a modified value (a string in this case), depending on conditions.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Write a function that takes a string as input.
- Hint 2: Use the drop() method to remove the first character from the string.
- Hint 3: Check if the remaining string starts with "yt" using the startsWith() method.
- Hint 4: If the string starts with "yt", use replaceFirst() to remove "yt" from the string and return it.
- Hint 5: If "yt" does not appear at index 1, return the original string.
- Hint 6: Test your function with different strings, such as "Scala", "yytade", and "ytsues".
Sample Solution:
Scala Code:
// Define an object named scala_basic
object scala_basic {
// Define a function named test with parameter str of type String, returning a String
def test(str: String): String = {
// Check if the string, excluding the first character, starts with "yt"
if (str.drop(1).startsWith("yt")) str.replaceFirst("yt", "")
// If the condition is not met, return the original string
else str
}
// 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 "yytade"
println("Result: " + test("yytade"))
// Print the result of calling test with the argument "ytsues"
println("Result: " + test("ytsues"))
}
}
Sample Output:
Result: Scala Result: yade Result: ytsues
Explanation:
Here is the break down of the said Scala code:
- object scala_basic {: This declares an object named scala_basic.
- def test(str: String): String = {: This line defines a function named test that takes a parameter str of type String and returns a String. The function performs a conditional check on the string.
- if (str.drop(1).startsWith("yt")) str.replaceFirst("yt", ""): This line checks if the substring of str from the second character onward starts with "yt." If true, it replaces the first occurrence of "yt" with an empty string using replaceFirst. Otherwise, it returns the original string.
- else str: If the condition in the if statement is not true, this part of the code is executed, and it returns the original string.
- }: 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("Scala")): This line calls the "test()" function with the argument "Scala" and prints the result to the console.
- println("Result: " + test("yytade")): Another call to the "test()" function with the argument "yytade."
- println("Result: " + test("ytsues")): Another call to the "test()" function with the argument "ytsues."
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check whether two given integer values are in the range 20..50 inclusive. Return true if 1 or other is in the said range otherwise false.
Next: Check the largest number among three given integers.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics