How to count the number of words in a string with Kotlin

1 Answer

0 votes
fun main() {
    val str = "kotlin c c++ java python rust"
    
    val words = str.split("\\s+".toRegex())
    
    val count = words.count()
    
    println("Number of words: $count")
}



 
/*
run:
 
Number of words: 6
 
*/

 



answered Dec 7, 2024 by avibootz
...