From 1cde22cdf050d0880f774e80e0808cade74d7bcf Mon Sep 17 00:00:00 2001 From: Qv1ko Date: Thu, 11 May 2023 23:45:03 +0200 Subject: [PATCH] Reto #19 - Java --- .../java/Qv1ko.java" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/java/Qv1ko.java" diff --git "a/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/java/Qv1ko.java" "b/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/java/Qv1ko.java" new file mode 100644 index 0000000000..b7d46cc281 --- /dev/null +++ "b/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/java/Qv1ko.java" @@ -0,0 +1,33 @@ +public class Qv1ko { + + public static void main(String[] args) { + textAnalysis("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis turpis vestibulum, elementum mi ac, gravida est. Phasellus ut mattis mauris, a aliquet odio. Integer nec erat sed libero efficitur interdum. Curabitur vitae dolor ac ligula sollicitudin suscipit. Aenean suscipit, sapien id elementum lobortis, turpis orci hendrerit risus, ut fermentum arcu nisl fermentum velit. Fusce id condimentum mi. Aliquam luctus sollicitudin nunc, id suscipit nibh efficitur efficitur. Etiam lacus leo, ullamcorper scelerisque eleifend nec, varius non orci. Morbi est felis, sodales faucibus ligula ut, consequat finibus nisl."); + } + + private static void textAnalysis(String text) { + int totalWords = 0, sentences = 0, lettersLongestWord = 0; + float totalWordLength = 0.0f; + String longestWord = ""; + for (String word : text.split(" ")) { + totalWords++; + if (Character.isLetter(word.charAt(word.length() - 1))) { + totalWordLength += word.length(); + if (word.length() > lettersLongestWord) { + longestWord = word; + lettersLongestWord = longestWord.length(); + } + } else { + totalWordLength += word.length() - 2; + if (word.length() > lettersLongestWord) { + longestWord = word.substring(0, word.length() - 1); + lettersLongestWord = longestWord.length(); + } + if(word.charAt(word.length() - 1) == '.') { + sentences++; + } + } + } + System.out.println("Total number of words: " + totalWords + "\nAverage word length: " + totalWordLength / totalWords+ "\nNumber of sentences: " + sentences + "\nLongest word: " + longestWord); + } + +}