Write a Scala program to make a new string made of p number of characters from the first of a given string and followed by p-1 number characters till the p is greater than zero

  • برمجة سكالا

Write a Scala program to make a new string made of p number of characters from the first of a given string and followed by p-1 number characters till the p is greater than zero.

الأجوبة

object Scala_String {
  def test(stng: String, n: Int): String = {
  var l = n
  var newstring = "";
    
  for (i<-n to 0 by -1)
  {
    newstring = newstring + stng.substring(0,l);
    l = l -1;
  }
  return newstring;
  }

  def main(args: Array[String]): Unit = {
     val str1 =  "welcome";
	   val rep_no=4;
     println("The given string is: "+str1);
	   println("Number of repetition characters and repetition: "+rep_no);
     println("The new string is: "+ test(str1, rep_no));
  }
}
Sample Output:
The given string is: welcome
Number of repetition characters and repetition: 4
The new string is: welcwelwew
هل كان المحتوى مفيد؟

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

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

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