There are following way to Count and find the Vowels in a String.
- First we convert given input string in lower case from toLowerCase() method.
- Now create a List in character type
- Add the vowels in that List.
- Now given input String in a character type from chars() method and convert this character as object type from mapToObj()
- Inside filter we check which character is vowel from contains ()
- For count total number of Vowels, we are using count()
- Now Print the Character.
import java.util.Arrays;
import java.util.List;
public class VowelCount {
public static void main(String[] args) {
String str = "welcome to java";
String lowerCase = str.toLowerCase();
List list = Arrays.asList('a', 'e', 'i', 'o', 'u');
System.out.println("Vowel character is :- ");
lowerCase.chars().mapToObj(c -> (char) c).filter(list::contains).forEach(System.out::println);
long VowelCount = lowerCase.chars().mapToObj(c -> (char) c).filter(list::contains).count();
System.out.println("Total number of Vowel is:- " + VowelCount);
}
}
Output :-
Vowel character is :-
e
o
e
o
a
a
Total number of Vowel is:- 6