Q:

Write a Scala program to create a new string repeating every character twice of a given string

0

Write a Scala program to create a new string repeating every character twice of a given string.

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): String = {
    val l = stng.length;
    var newstring = "";
    for (i <- 0 to l - 1) {
      newstring += stng.substring(i, i + 1) + stng.substring(i, i + 1);
    }
    newstring;
  }

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

Sample Output:

The given string is: welcome
The new string is: wweellccoommee

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