Scala Programming: Create a new string with the last char added at the front and back of a given string of length 1 or more
Write a Scala program to create a new string with the last char added at the front and back of a given string of length 1 or more.
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 with parameters and return values.
- String Operations: Using methods like charAt() to get specific characters from a string.
- String Length: Using the length property to determine the number of characters in a string.
- String Concatenation: Using the + operator to concatenate strings.
- Main Method: Understanding the structure of the main() function in Scala.
- Printing to Console: Using println() to display results.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Define an object in Scala to contain the function.
- Hint 2: Create a function that accepts a string as an argument.
- Hint 3: Use the length property to find the length of the string.
- Hint 4: Use the charAt() method to get the last character of the string.
- Hint 5: Concatenate the last character at the front and back of the string.
- Hint 6: Test your function with various string inputs, including single-character strings.
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 =
{
// Calculate the length of the input string and assign it to the variable len
val len = str1.length
// Retrieve the last character of the input string and assign it to the variable last
val last = str1.charAt(len - 1)
// Concatenate the last character, the input string, and the last character again
last + str1 + last
}
// 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: aScalaa Result: dabcdd Result: babb 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 2. If true, it returns the string as is. If false, it takes the substring of the first two characters and repeats it 4 times.
- if (str1.length < 2) str1 else str1.substring(0, 2) 4: This is a conditional expression inside the test function. If the length of the string str1 is less than 2, it returns the string as is. If false, it takes the substring of the first two characters using str1.substring(0, 2) and repeats it 4 times using the operator.
- 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: Create a new string which is 4 copies of the 2 front characters of a given string.If the given string length is less than 2 return the original string.
Next: Check whether a given positive number is a multiple of 3 or a multiple of 7.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics