In this post, we will see how to find and count vowels in a string.
Table of Contents
Find Vowels in a String
If any character in String satisfy below condition then it is vowel and we will add it to Hashset.
character==’a’ || character==’A’ || character==’e’ || character==’E’ ||
character==’i’ || character==’I’ || character==’o’ || character==’O’ ||
character==’u’ || character==’U’
character==’i’ || character==’I’ || character==’o’ || character==’O’ ||
character==’u’ || character==’U’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package org.arpit.java2blog; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class VowelFinder { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an String : "); String str = scanner.next(); Set<Character> set=new HashSet<Character>(); for (int i = 0; i < str.length(); i++) { char c=str.charAt(i); if(isVowel(c)) { set.add(c); } } System.out.println("Vowels are:"); for (Character c:set) { System.out.print(" "+c); } scanner.close(); } public static boolean isVowel(char character) { if(character=='a' || character=='A' || character=='e' || character=='E' || character=='i' || character=='I' || character=='o' || character=='O' || character=='u' || character=='U'){ return true; }else{ return false; } } } |
Output:
Enter an String : Java2blog
Vowels are:
a o
Vowels are:
a o
Explanation
- Iterate over String
str
and check eachCharacter
isvowel
or not based on below condition
12345if(character=='a' || character=='A' || character=='e' || character=='E' ||character=='i' || character=='I' || character=='o' || character=='O' ||character=='u' || character=='U') - If it is vowel, then print the vowel
Count number of Vowels in the String
Here is the program to count number of Vowels in the String.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package org.arpit.java2blog; import java.util.Scanner; public class VowelCounter { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an String : "); String str = scanner.next(); int countVowels = countVowels(str); System.out.println("Number of vowels: "+countVowels); scanner.close(); } public static int countVowels(String str) { int count = 0; for (int i = 0; i < str.length(); i++) { char character =str.charAt(i); if(character=='a' || character=='A' || character=='e' || character=='E' || character=='i' || character=='I' || character=='o' || character=='O' || character=='u' || character=='U'){ count++; } } return count; } } |
Output:
Enter an String : Java2blog
Number of vowels: 3
Number of vowels: 3
Explanation
- Declare
count
variable and initialize it with 0. - Iterate over String
str
and check eachCharacter
isvowel
or not based on below condition
12345if(character=='a' || character=='A' || character=='e' || character=='E' ||character=='i' || character=='I' || character=='o' || character=='O' ||character=='u' || character=='U') - If it is vowel, then increment the
count
- Once loop is complete, then return the
count
.
That’s all about how to find vowels in a string in java.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.