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.
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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer