Partie 1 : Création de la classe TextProcessor complète avec les méthodes demandées.
This commit is contained in:
160
com/humanbooster/exercices/TextProcessor.java
Normal file
160
com/humanbooster/exercices/TextProcessor.java
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user