Q:

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

0

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.

All Answers

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

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

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