Bonus Exercise 4 completed.

This commit is contained in:
2026-04-23 22:00:26 +03:30
parent aac792e1f2
commit e2a62ea34e
+20 -4
View File
@@ -76,11 +76,27 @@ public class BonusExercises {
*/
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
// todo
String regex = "[^a-zA-Z]+";
//Splitting the string by anything except words to check each one
String[] words = string.split(regex);
for (String word : words) {
if (word.length() >= 3) {
//Case-insensitive
String lower = word.toLowerCase();
String reversed = new StringBuilder(lower).reverse().toString();
//Checking if it's palindrome or not
if (lower.equals(reversed)) {
list.add(word);
}
}
}
return list;
}
public static void main(String[] args) {
// you can test your code here
}
public static void main(String[] args) {}
}