Write a Scala program to count the number of triples (characters appearing three times in a row) in a given string

  • برمجة سكالا

Write a Scala program to count the number of triples (characters appearing three times in a row) in a given string.

الأجوبة

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
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...