From fb1cad58af3fdc748e330a696a778f42d5b5d990 Mon Sep 17 00:00:00 2001 From: Vincent Guillet Date: Tue, 29 Apr 2025 13:39:39 +0200 Subject: [PATCH] =?UTF-8?q?Partie=201=20:=20Cr=C3=A9ation=20de=20la=20clas?= =?UTF-8?q?se=20TextProcessor=20compl=C3=A8te=20avec=20les=20m=C3=A9thodes?= =?UTF-8?q?=20demand=C3=A9es.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- com/humanbooster/exercices/TextProcessor.java | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 com/humanbooster/exercices/TextProcessor.java diff --git a/com/humanbooster/exercices/TextProcessor.java b/com/humanbooster/exercices/TextProcessor.java new file mode 100644 index 0000000..f36bc44 --- /dev/null +++ b/com/humanbooster/exercices/TextProcessor.java @@ -0,0 +1,160 @@ +package com.humanbooster.exercices; + +public class TextProcessor { + + public static void main(String[] args) { + + TextProcessor processor = new TextProcessor(); + + String[] tests = { + "s", "word", "hello world", "lorem ipsum je connais pas la suite", + " s ", " word ", " hello world ", " lorem ipsum je connais pas la suite ", + "\r\n\t s \r\n\t", "\tword\n", "\rhello world\r", " \nlorem ipsum je connais pas la suite\t", + "lorem\nipsum\rje\tconnais pas \n la \r suite \t !" + }; + + try { + for (String text : tests) { + System.out.println("Nombre de mots dans " + "'" + text.trim() + "'" + " : " + processor.countWords(text)); + } + } catch (IllegalArgumentException e) { + System.out.println("Erreur : " + e.getMessage()); + } + } + + // 1 + + /** + * Compte le nombre de mots dans une chaîne de caractères. + * + * @param text La chaîne de caractères à analyser. + * @return Le nombre de mots dans la chaîne. + */ + public int countWords(String text) { + if (text == null || text.isEmpty()) throw new IllegalArgumentException("Text cannot be null or empty"); + return text.trim().split("\\s+").length; + } + + /** + * Compte le nombre d'occurrences d'un mot dans une chaîne de caractères. + * + * @param text La chaîne de caractères à analyser. + * @param word Le mot à rechercher. + * @return Le nombre d'occurrences du mot dans la chaîne. + */ + public int countOccurrences(String text, String word) { + if (text == null || text.isEmpty() || word == null || word.isEmpty()) throw new IllegalArgumentException("Text and word cannot be null or empty"); + String[] words = text.trim().split("\\s+"); + int count = 0; + for (String w : words) if (w.equalsIgnoreCase(word)) count++; + return count; + } + + /** + * Inverse une chaîne de caractères. + * + * @param text La chaîne de caractères à inverser. + * @return La chaîne inversée. + */ + public String reverse(String text) { + if (text == null || text.isEmpty()) throw new IllegalArgumentException("Text cannot be null or empty"); + StringBuilder reversed = new StringBuilder(text); + return reversed.reverse().toString(); + } + + /** + * Supprime les doublons d'une chaîne de caractères. + * + * @param text La chaîne de caractères à traiter. + * @return La chaîne sans doublons. + */ + public String removeDuplicates(String text){ + if (text == null || text.isEmpty()) throw new IllegalArgumentException("Text cannot be null or empty"); + String[] words = text.trim().split("\\s+"); + StringBuilder result = new StringBuilder(); + for (String word : words) if (result.indexOf(word) == -1) result.append(word).append(" "); + return result.toString().trim(); + } + + /** + * Extrait les mots d'une chaîne de caractères. + * + * @param text La chaîne de caractères à traiter. + * @return Un tableau de mots extraits de la chaîne. + */ + public String[] extractWords(String text) { + if (text == null || text.isEmpty()) throw new IllegalArgumentException("Text cannot be null or empty"); + return text.trim().split("\\s+"); + } + + // 2 + + /** + * Transforme la première lettre de chaque mot en majuscule. + * + * @param text La chaîne de caractères à traiter. + * @return La chaîne avec la première lettre de chaque mot en majuscule. + */ + public String toTitleCase(String text) { + if (text == null || text.isEmpty()) throw new IllegalArgumentException("Text cannot be null or empty"); + String[] words = text.trim().split("\\s+"); + StringBuilder titleCase = new StringBuilder(); + for (String word : words) { + if (word.length() > 0) { + titleCase.append(Character.toUpperCase(word.charAt(0))); + titleCase.append(word.substring(1).toLowerCase()); + titleCase.append(" "); + } + } + return titleCase.toString().trim(); + } + + /** + * Transforme une chaîne de caractères en camel case. + * + * @param text La chaîne de caractères à traiter. + * @return La chaîne en camel case. + */ + public String toCamelCase(String text){ + if (text == null || text.isEmpty()) throw new IllegalArgumentException("Text cannot be null or empty"); + String[] words = text.trim().split("\\s+"); + StringBuilder camelCase = new StringBuilder(); + for (String word : words) { + if (word.length() > 0) { + camelCase.append(Character.toUpperCase(word.charAt(0))); + camelCase.append(word.substring(1).toLowerCase()); + } + } + return camelCase.toString(); + } + + /** + * Transforme une chaîne de caractères en snake case. + * + * @param text La chaîne de caractères à traiter. + * @return La chaîne en snake case. + */ + public String toSnakeCase(String text){ + if (text == null || text.isEmpty()) throw new IllegalArgumentException("Text cannot be null or empty"); + String[] words = text.trim().split("\\s+"); + StringBuilder snakeCase = new StringBuilder(); + for (String word : words) { + if (word.length() > 0) { + snakeCase.append(word.toLowerCase()); + snakeCase.append("_"); + } + } + return snakeCase.toString().substring(0, snakeCase.length() - 1); + } + + /** + * Supprime les espaces d'une chaîne de caractères. + * + * @param text La chaîne de caractères à traiter. + * @return La chaîne sans espaces. + */ + public String compress(String text) { + if (text == null || text.isEmpty()) throw new IllegalArgumentException("Text cannot be null or empty"); + return text.trim(); + } +}