Write a Scala program to repeat a specific number of characters for specific number of times from the last part of a given string

  • برمجة سكالا

Write a Scala program to repeat a specific number of characters for specific number of times from the last part of a given string.

الأجوبة

object Scala_String {
  def test(stng: String, no_repeat: Int): String = {
    val l = stng.length;
    var new_word = "";
    for (i <- 0 to no_repeat - 1) {
      new_word += stng.substring(l - no_repeat, l);
    }
    new_word;
  }

  def main(args: Array[String]): Unit = {
    val str1 = "string";
    val no_char = 3;
    println("The given string is: " + str1);
    println("The new string after repetition: " + test(str1, no_char));
  }
}

Sample Output:

The given string is: string
The new string after repetition: inginging
هل كان المحتوى مفيد؟

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

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

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