class CountCharacters_Kotlin {
companion object {
fun countCharacters(s: String) {
val arr = s.toCharArray()
var letter = 0
var spaces = 0
var numbers = 0
var otherchars = 0
for (i in s.indices) {
when {
arr[i].isLetter() -> letter++
arr[i].isDigit() -> numbers++
arr[i].isWhitespace() -> spaces++
else -> otherchars++
}
}
println("letter: $letter")
println("space: $spaces")
println("number: $numbers")
println("other: $otherchars")
}
@JvmStatic
fun main(args: Array<String>) {
val s = "Ko12tlin \$% Prog()ramming 99 !!!"
countCharacters(s)
}
}
}
/*
run:
letter: 17
space: 10
number: 4
other: 7
*/