Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

object scala_basic {
  def test(str1: String): String = 
    {
    val len = str1.length
    val last = str1.charAt(len - 1)
    last + str1 + last
    }
     
   def main(args: Array[String]): Unit = {
      println("Result: " + test("Scala"));
      println("Result: " + test("abcd"));
      println("Result: " + test("ab"));
      println("Result: " + test("a"));
    }
}

Sample Output:

Result: aScalaa
Result: dabcdd
Result: babb
Result: aaa

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now