w3resource

Scala Programming: Convert the last 4 characters of a given string in upper case

Scala Programming Basic Exercise-25 with Solution

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.

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?



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://w3resource.com/scala-exercises/basic/scala-basic-exercise-25.php