Scala Programming: Count the number of triples (characters appearing three times in a row) in a given string
Scala Programming String Exercise-43 with Solution
Write a Scala program to count the number of triples (characters appearing three times in a row) in a given string.
Sample Solution:
Scala Code:
object Scala_String {
def test(stng: String): Int = {
var l = stng.length();
var ctr = 0;
for (i <- 0 to l-3)
{
var tmp = stng.charAt(i);
if (tmp == stng.charAt(i+1) && tmp == stng.charAt(i+2))
ctr=ctr+1;
}
return ctr;
}
def main(args: Array[String]): Unit = {
val str1 = "welllcommmmeee";
println("The given string is: "+str1);
println("The number of triples in the string is: "+test(str1));
}
}
Sample Output:
The given string is: welllcommmmeee The number of triples in the string is: 4
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to make a new string with each character of just before and after of a non-empty substring whichever it appears in a non-empty given string.
Next: Write a Scala program to check whether a specified character is happy or not. A character is happy when the same character appears to its left or right in a 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