refactor: CheckVowels
(#5393)
This commit is contained in:
parent
a5f57fbfde
commit
580aa0c9c5
@ -1,7 +1,5 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@ -10,24 +8,24 @@ import java.util.Set;
|
||||
* alphabet. Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order
|
||||
*/
|
||||
public final class CheckVowels {
|
||||
private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o', 'u');
|
||||
|
||||
private CheckVowels() {
|
||||
}
|
||||
|
||||
private static final Set<Character> VOWELS = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
|
||||
|
||||
/**
|
||||
* Check if a string is has vowels or not
|
||||
* Checks if a string contains any vowels.
|
||||
*
|
||||
* @param input a string
|
||||
* @return {@code true} if given string has vowels, otherwise {@code false}
|
||||
* @param input a string to check
|
||||
* @return {@code true} if the given string contains at least one vowel, otherwise {@code false}
|
||||
*/
|
||||
public static boolean hasVowels(String input) {
|
||||
if (input == null) {
|
||||
if (input == null || input.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
input = input.toLowerCase();
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
if (VOWELS.contains(input.charAt(i))) {
|
||||
|
||||
for (char c : input.toLowerCase().toCharArray()) {
|
||||
if (VOWELS.contains(c)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,15 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
public class CheckVowelsTest {
|
||||
class CheckVowelsTest {
|
||||
|
||||
@Test
|
||||
public void isVowel() {
|
||||
assertTrue(CheckVowels.hasVowels("foo"));
|
||||
assertTrue(CheckVowels.hasVowels("bar"));
|
||||
assertFalse(CheckVowels.hasVowels("why"));
|
||||
assertFalse(CheckVowels.hasVowels("myths"));
|
||||
@ParameterizedTest
|
||||
@CsvSource({"'foo', true", "'bar', true", "'why', false", "'myths', false", "'', false", "'AEIOU', true", "'bcdfghjklmnpqrstvwxyz', false", "'AeIoU', true"})
|
||||
void testHasVowels(String input, boolean expected) {
|
||||
assertEquals(CheckVowels.hasVowels(input), expected);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user