w3resource

Scala Programming: Check the number of appearances of the two substrings appear any where in the string


Write a Java program to check the number of appearances of the two substrings appear any where in the string.

Sample Solution:

Scala Code:

object Scala_String {
  def test(stng: String): Boolean = {
  var l = stng.length;
  var st_the = 0;
  var st_is = 0;
  for (i <- 0 to l-1) 
  {
    if (i < l - 2) 
	{
      var tmp = stng.substring(i,i+3);
      if (tmp.equals("the"))
        st_the = st_the +1;
    }
    if (i < l - 1) 
	{
      var tmp2 = stng.substring(i,i+2);
      if (tmp2.equals("is"))
        st_is = st_is + 1;
    }
  }
  if (st_the == st_is)
    return true;
  else
    return false;
  }
  def main(args: Array[String]): Unit = {
      var str1 =  "Thisisthethesis";
      println("The given string is: "+str1);
      println("Are the appearance of 'the' and 'is' equal? "+test(str1));
    
      str1 =  "Thisisthethes";
      println("The given string is: "+str1);
      println("Are the appearance of 'the' and 'is' equal? "+test(str1));
  }
}

Sample Output:

The given string is: Thisisthethesis
Are the appearance of 'the' and 'is' equal? false
The given string is: Thisisthethes
Are the appearance of 'the' and 'is' equal? true

Go to:


PREV : Write a Scala program to calculate the sum of the numbers appear in a given string.
NEXT : Scala Sets Exercises Home.

Scala Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.