Write a Scala program to add a string with specific number of times separated by a substring.
object Scala_String { def test(main_str: String, sep_str: String, ctr: Int): String = { var new_word = ""; for (i <- 0 to ctr - 1) { if (i < ctr - 1) new_word = new_word + (main_str + sep_str) else new_word += main_str; } return new_word; } def main(args: Array[String]): Unit = { val str1 = "try"; val str2 = "best"; var ctr = 3; println("The given strings are: " + str1 + " and " + str2); println("Number to times to be repeat: " + ctr); println("The new string is: " + test(str1, str2, ctr)); } }
Sample Output:
The given strings are: try and best Number to times to be repeat: 3 The new string is: trybesttrybesttry
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