import scala.util.matching.Regex
object CountCharacters {
private def countMatches(text: String, pattern: String): Int = {
new Regex(pattern).findAllIn(text).length
}
def countCharacters(text: String): (Int, Int, Int, Int) = {
val uppercase = countMatches(text, "[A-Z]")
val lowercase = countMatches(text, "[a-z]")
val digits = countMatches(text, "\\d")
val special = countMatches(text, "[^A-Za-z0-9]")
(uppercase, lowercase, digits, special)
}
def main(args: Array[String]): Unit = {
val s = "Programming&AI@2026!"
val (u, l, d, spc) = countCharacters(s)
println(s"Uppercase: $u")
println(s"Lowercase: $l")
println(s"Digits: $d")
println(s"Special characters: $spc")
}
}
/*
run:
Uppercase: 3
Lowercase: 10
Digits: 4
Special characters: 3
*/