Skip to content

Commit 7e3cdda

Browse files
authored
BAEL-5562 Check if character is vowel (#12221)
1 parent ee9b5a4 commit 7e3cdda

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

core-java-modules/core-java-string-operations-4/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
<artifactId>commons-lang3</artifactId>
3131
<version>${apache-commons-lang3.version}</version>
3232
</dependency>
33+
<dependency>
34+
<groupId>org.assertj</groupId>
35+
<artifactId>assertj-core</artifactId>
36+
<version>${assertj.version}</version>
37+
<scope>test</scope>
38+
</dependency>
3339
</dependencies>
3440

3541
<build>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.checkvowels;
2+
3+
import java.util.regex.Pattern;
4+
5+
public class CheckVowels {
6+
private static final String VOWELS = "aeiouAEIOU";
7+
private static final Pattern VOWELS_PATTERN = Pattern.compile("[aeiou]", Pattern.CASE_INSENSITIVE);
8+
9+
public static boolean isInVowelsString(char c) {
10+
return VOWELS.indexOf(c) != -1;
11+
}
12+
13+
public static boolean isInVowelsString(String c) {
14+
return VOWELS.contains(c);
15+
}
16+
17+
public static boolean isVowelBySwitch(char c) {
18+
switch (c) {
19+
case 'a':
20+
case 'e':
21+
case 'i':
22+
case 'o':
23+
case 'u':
24+
case 'A':
25+
case 'E':
26+
case 'I':
27+
case 'O':
28+
case 'U':
29+
return true;
30+
default:
31+
return false;
32+
}
33+
}
34+
35+
public static boolean isVowelByRegex(String c) {
36+
return VOWELS_PATTERN.matcher(c).matches();
37+
}
38+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.checkvowels;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static com.baeldung.checkvowels.CheckVowels.*;
6+
import static org.assertj.core.api.Assertions.*;
7+
8+
class CheckVowelsUnitTest {
9+
10+
@Test
11+
void givenAVowelCharacter_thenInVowelString() {
12+
assertThat(isInVowelsString('e')).isTrue();
13+
}
14+
15+
@Test
16+
void givenAConsonantCharacter_thenNotInVowelString() {
17+
assertThat(isInVowelsString('z')).isFalse();
18+
}
19+
20+
@Test
21+
void givenAVowelString_thenInVowelString() {
22+
assertThat(isInVowelsString("e")).isTrue();
23+
}
24+
25+
@Test
26+
void givenAConsonantString_thenNotInVowelString() {
27+
assertThat(isInVowelsString("z")).isFalse();
28+
}
29+
30+
@Test
31+
void givenAVowelCharacter_thenInVowelSwitch() {
32+
assertThat(isVowelBySwitch('e')).isTrue();
33+
}
34+
35+
@Test
36+
void givenAConsonantCharacter_thenNotInVowelSwitch() {
37+
assertThat(isVowelBySwitch('z')).isFalse();
38+
}
39+
40+
@Test
41+
void givenAVowelString_thenInVowelPattern() {
42+
assertThat(isVowelByRegex("e")).isTrue();
43+
assertThat(isVowelByRegex("E")).isTrue();
44+
}
45+
46+
@Test
47+
void givenAVowelCharacter_thenInVowelPattern() {
48+
assertThat(isVowelByRegex(Character.toString('e'))).isTrue();
49+
assertThat(isVowelByRegex("E")).isTrue();
50+
}
51+
}

0 commit comments

Comments
 (0)