Generation

generate functionTue, 03 Jan 2023

Create code to search for digits in a sentence and return numbers only in a list array. Return the results in array

fun main(args: Array<String>) { val sentence = "I am 22 years old because I was born in 1994" fun getNumbers(sentence: String): List<Int> { val numbers = mutableListOf<Int>() for (word in sentence.split(" ")) { if (word.matches(Regex("\\d+"))) { numbers.add(word.toInt()) } } return numbers } println(getNumbers(sentence)) }

Questions about programming?Chat with your personal AI assistant