In this post, we will see how to find and count vowels in a string.
[toc]
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’
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
strand check eachCharacterisvowelor not based on below conditionif(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.
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
countvariable and initialize it with 0. - Iterate over String
strand check eachCharacterisvowelor not based on below conditionif(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.