Scala Programming: Repeat a specific number of characters for specific number of times from the last part of a string
Scala Programming String Exercise-35 with Solution
Write a Scala program to repeat a specific number of characters for specific number of times from the last part of a given string.
Sample Solution:
Scala Code:
object Scala_String {
def test(stng: String, no_repeat: Int): String = {
val l = stng.length;
var new_word = "";
for (i <- 0 to no_repeat - 1) {
new_word += stng.substring(l - no_repeat, l);
}
new_word;
}
def main(args: Array[String]): Unit = {
val str1 ="string";
val no_char = 3;
println("The given string is: " + str1);
println("The new string after repetition: " + test(str1, no_char));
}
}
Sample Output:
The given string is: string The new string after repetition: inginging
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to add a string with specific number of times separated by a substring.
Next: Write a Scala program to create a new string from a given string after removing the 2nd character from the substring of length three starting with 'z' and ending with 'g' presents in the said string.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework