Skip to content

Commit

Permalink
Reto #19 - Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
marchdz committed May 12, 2023
1 parent c1c5398 commit c11095e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Retos/Reto #19 - ANÁLISIS DE TEXTO [Media]/kotlin/marchdz.kt
Original file line number Diff line number Diff line change
@@ -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("")
}

0 comments on commit c11095e

Please sign in to comment.