← Coding round practice

Count Words in a Sentence

EasyStrings

The problem

Count the number of words in a sentence (words separated by spaces).

Sample input

the quick brown fox

Sample output

4
Show the solution · 8 languages
fun main() {
    val s = "the quick brown fox"
    println(s.trim().split(Regex("\\s+")).size)
}