Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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:

object scala_basic {
    def test(str1: String): String = {
    str1.take(str1.length() - 4) + str1.drop(str1.length() - 4).toUpperCase()
   }
   def main(args: Array[String]): Unit = {
      println("Result: " + test("Scala"));
      println("Result: " + test("Python"));
      println("Result: " + test("abc"));      
    }
  }

Sample Output:

Result: SCALA
Result: PyTHON
Result: ABC

Scala Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Scala program to check whether two given positive integers have the same last digit.
Next: Write a Scala program to create a new string which is n (non-negative integer ) copies of a given string.

What is the difficulty level of this exercise?