From c11095e39b821dc6749fbc780e8ad29bc752f46d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Hern=C3=A1ndez?= Date: Fri, 12 May 2023 02:16:18 +0200 Subject: [PATCH] Reto #19 - Kotlin --- .../kotlin/marchdz.kt" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/kotlin/marchdz.kt" diff --git "a/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/kotlin/marchdz.kt" "b/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/kotlin/marchdz.kt" new file mode 100644 index 0000000000..30a5294e7b --- /dev/null +++ "b/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/kotlin/marchdz.kt" @@ -0,0 +1,28 @@ +fun analyzeText(text: String) { + val sentenceCount = text.split(".", ":", ";", "!", "?") + .filter { it.isNotBlank() }.size + val words = text.split(" ").filter { it.isNotBlank() }.toMutableList() + val wordsCount = words.size + + for ((index, word) in words.withIndex()) { + words[index] = word.replace(Regex("[,.:;!¡¿?]"), "") + } + + val wordsAverageLength = if (wordsCount > 0) words.sumOf(String::length) / wordsCount else 0 + val longestWord = if (words.isNotEmpty()) words.maxBy(String::length) else "" + + println(""" + ======================================= + Número total de palabras: $wordsCount + Longitud media de las palabras: $wordsAverageLength + Número de oraciones del texto: $sentenceCount + Palabra más larga: $longestWord + ======================================= + """.trimIndent()) +} + +fun main() { + analyzeText("Esto es una prueba; siempre hay que probar. ¿Ha funcionado? ¡Seguro!") + analyzeText("t") + analyzeText("") +} \ No newline at end of file