Scala Programming: Create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back
Write a Scala program to create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3, use whatever characters are there.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Scala Objects: Understanding how to define objects using the object keyword.
- Functions in Scala: How to create functions that take parameters and return values.
- String Manipulation: Using substring() to extract parts of a string.
- String Repetition: Using the * operator to repeat a string.
- Conditionals: Using if-else statements to check conditions and make decisions.
- String Concatenation: How to combine strings using the + operator.
- Main Method: The main() method is the entry point in a Scala program.
- Printing to Console: Using println() to output results to the console.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Define an object to hold your Scala code.
- Hint 2: Write a function that accepts a string as an argument.
- Hint 3: If the length of the string is less than 3, repeat the string three times using the * operator.
- Hint 4: If the length of the string is 3 or more, extract the first 3 characters and add them at both the front and back of the original string.
- Hint 5: Test your function with different strings to make sure it works correctly.
Sample Solution:
Scala Code:
// Define an object named scala_basic
object scala_basic {
// Define a function named test with parameter str1 of type String, returning a String
def test(str1: String): String =
{
// Check if the length of the input string is less than 3
if (str1.length < 3)
// If true, repeat the string three times using the * operator
str1 * 3
else
// If false, concatenate the substring of the first three characters, the input string, and the substring of the first three characters
str1.substring(0, 3) + str1 + str1.substring(0, 3)
}
// 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 "abcd"
println("Result: " + test("abcd"))
// Print the result of calling test with the argument "ab"
println("Result: " + test("ab"))
// Print the result of calling test with the argument "a"
println("Result: " + test("a"))
}
}
Sample Output:
Result: ScaScalaSca Result: abcabcdabc Result: ababab Result: aaa
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. The function checks if the length of the input string is less than 3. If true, it repeats the string three times using the * operator. If false, it concatenates the substring of the first three characters, the input string, and the substring of the first three characters.
- if (str1.length < 3) str1 3: This is a conditional expression inside the "test()" function. If the length of the string str1 is less than 3, it returns the string repeated three times using the operator.
- else str1.substring(0, 3) + str1 + str1.substring(0, 3): If the length of the string is 3 or more, it concatenates the substring of the first three characters, the input string, and the substring of the first three characters.
- 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", concatenates the result with the string "Result: ", and prints the entire string to the console.
- println("Result: " + test("abcd")): Similar to the previous line, this calls the "test()" function with the argument "abcd".
- println("Result: " + test("ab")): Another call to the "test()" function with the argument "ab".
- println("Result: " + test("a")): Another call to the "test()" function with the argument "a".
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check whether a given positive number is a multiple of 3 or a multiple of 7.
Next: Check whether a given string starts with 'Sc' or not.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics