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

  • برمجة سكالا

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
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...