Kotlinlearncs.online LogoJava
Return to List

Homework: Word Lengths With Map

Created By: Geoffrey Challen
/ Version: 2021.9.0

Given a String? containing words separated by the " " (space) character, write a method wordLengths that return a Map<String, Int> mapping each word that is contained in the String to its length. require that the passed String? is not null.

So, for example, given the String "Wow that is amazing", you would return a map with four mappings: "Wow" to 3, "that" to 4, "is" to 2, and "amazing" to 7.

Note that you should not need any import statements, since Lists are built-in to Kotlin and always available.

Related Lessons

Stuck? You may find these lessons helpful: