Compare commits

...

2 Commits

Author SHA1 Message Date
BadOfficer 531238a935 update lab5 2023-05-31 11:13:34 +03:00
BadOfficer 879d207e0d update lab5 2023-05-31 10:49:23 +03:00
1 changed files with 3 additions and 5 deletions

View File

@ -25,19 +25,17 @@ public class Sentence {
allWords.append(sentence).append(" "); allWords.append(sentence).append(" ");
} }
String[] words = allWords.toString().split("\\W+"); // Remove punctuation marks String[] words = allWords.toString().split("\\W+");
HashSet<String> uniqueWords = new HashSet<>(); HashSet<String> uniqueWords = new HashSet<>();
for (String word : words) { for (String word : words) {
if (!word.isEmpty()) { // Ignore empty words uniqueWords.add(word.toLowerCase());
uniqueWords.add(word.toLowerCase()); // Convert word to lowercase and add to HashSet
}
} }
String[] uniqueSortedWords = uniqueWords.toArray(new String[0]); String[] uniqueSortedWords = uniqueWords.toArray(new String[0]);
Arrays.sort(uniqueSortedWords); // Sort unique words by the first letter Arrays.sort(uniqueSortedWords);
for (String word : uniqueSortedWords) { for (String word : uniqueSortedWords) {
System.out.println(word); System.out.println(word);