Scala Programming: Convert the last 4 characters of a given string in upper case
Write a Scala program to convert the last 4 characters of a given string in upper case. If the length of the string has less than 4 then uppercase all the characters.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, make sure you are familiar with the following concepts:
- String Manipulation: Understand how to manipulate strings, including extracting substrings using methods like take and drop.
- String Length: Learn how to find the length of a string using str.length() to help with the logic for extracting portions of the string.
- Uppercase Conversion: Be familiar with the toUpperCase() method, which converts all characters in a string to uppercase.
- Conditional Logic: If the string has less than 4 characters, you’ll need to convert the entire string to uppercase, so understand conditional handling in Scala.
Hints (Try Before Looking at the Solution!)
Consider the following hints to guide you through solving this exercise:
- Hint 1: Use the take method to get all characters except the last 4 characters from the string.
- Hint 2: Use the drop method to get the last 4 characters of the string, and convert them to uppercase using toUpperCase().
- Hint 3: If the string length is less than 4, apply the toUpperCase() method to the entire string.
- Hint 4: Concatenate the substring of the string before the last 4 characters with the uppercase version of the last 4 characters.
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 String
def test(str1: String): String = {
// Take the substring of str1 excluding the last 4 characters and concatenate it with the uppercase of the last 4 characters
str1.take(str1.length() - 4) + str1.drop(str1.length() - 4).toUpperCase()
}
// 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 "Python"
println("Result: " + test("Python"))
// Print the result of calling test with the argument "abc"
println("Result: " + test("abc"))
}
}
Sample Output:
Result: SCALA Result: PyTHON Result: ABC
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): String = {: This line defines a function named test that takes a parameter str1 of type String and returns a String.
- str1.take(str1.length() - 4) + str1.drop(str1.length() - 4).toUpperCase(): This line creates a new string by taking the substring of str1 excluding the last 4 characters and concatenating it with the uppercase version of the last 4 characters.
- }: 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("Python")): Another call to the "test()" function with the argument "Python".
- println("Result: " + test("abc")): Another call to the "test()" function with the argument "abc".
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check whether two given positive integers have the same last digit.
Next: Create a new string which is n (non-negative integer ) copies of a given string.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics