Scala Programming: Create a new string repeating every character twice of a given string
Scala Programming String Exercise-39 with Solution
Write a Scala program to create a new string repeating every character twice of a given string.
Sample Solution:
Scala Code:
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
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Java program to check whether two strings of length 3 and 4 appear in same number of times in a given string
Next: Write a Scala program to make a new string from two given string in such a way that, each character of two string will come respectively.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/scala-exercises/string/scala-string-exercise-39.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics