OneCompiler

read a char from user and count the num of occurrences

142

Q2 Write a program to read a character from user and count the number of occurrences ofthat character.
object char
{
def main(args: Array[String])
{
println("Enter a character to count its occurrences: ")
var char = scala.io.StdIn.readChar()

  println("Enter a String: ")
  var str = scala.io.StdIn.readLine()

  var count = 0
  for(i <- 0 until str.length)
  {
     if(char == str(i))
        count += 1
  }

  println("Number of occurrences of given char in a string: " + count)

}
}