Q:

Write a Scala program to create a new string from a given string

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

object Scala_String {
  def test(stng: String): String = {
  val len = stng.length;
  var newformstring = "";
  for (i <- 0 to len-1) 
  {
    newformstring += stng.substring(i,i+1);
    if (i > 0 && i < len-1) 
	{
      if (stng.charAt(i-1) == 'z' && stng.charAt(i+1) == 'g')
        newformstring = newformstring.substring(0,newformstring.length()-1);
    }
  }
  newformstring;
  }
  def main(args: Array[String]): Unit = {
      var str1 =  "zzgkitandkatcaketoket";
      println("The given string is: "+str1);
      println("The new string is: "+test(str1));
      
      str1 =  "kitandkazzgtcaketoket";
      println("The given string is: "+str1);
      println("The new string is: "+test(str1));
    
      str1 =  "kitandkatcaketoketzzg";
      println("The given string is: "+str1);
      println("The new string is: "+test(str1));
  }
}

Sample Output:

The given string is: zzgkitandkatcaketoket
The new string is: zgkitandkatcaketoket
The given string is: kitandkazzgtcaketoket
The new string is: kitandkazgtcaketoket
The given string is: kitandkatcaketoketzzg
The new string is: kitandkatcaketoketzg

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now