w3resource

Scala Programming: Exchange the first and last characters in a given string and return the new string

Scala Programming Basic Exercise-8 with Solution

Write a Scala program to exchange the first and last characters in a given string and return the new string.

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
      
      // Check if the length is less than or equal to 1; if true, return the string as is
      if (len <= 1) 
        str1
      else 
        // If false, rearrange the string by moving the last character to the front and vice versa
        str1.charAt(len - 1) + str1.substring(1, len - 1) + str1.charAt(0)
    }   

   // 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: acalS
Result: dbca
Result: ba
Result: a

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 rearranges the input string by moving the last character to the front and the first character to the end.
  • val len = str1.length: This line calculates the length of the input string str1 and assigns it to the variable len.
  • if (len <= 1) str1 else str1.charAt(len - 1) + str1.substring(1, len - 1) + str1.charAt(0): This is a conditional expression inside the "test()" function. If the length of the string is less than or equal to 1, it returns the string as is. Otherwise, it rearranges the string by concatenating the last character, the substring from index 1 to len - 1, and the first character.
  • 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: Remove the character in a given position of a given string. The given position will be in the range 0...string length -1 inclusive.
Next: 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.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.