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?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework