Home >>Java Programs >Java Program to count the total number of vowels and consonants in a string
In this example, we will create a java program to count the total number of vowels and consonants present in the given string.
The characters a, e, i, o, u are known as vowels in the English alphabet and any character other than that is known as the consonant.
public class Main
{
public static void main(String[] args)
{
int vCount = 0, cCount = 0;
String str = "Abhimanyu";
str = str.toLowerCase();
for(int i = 0; i < str.length(); i++)
{
if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u')
{
vCount++;
}
else if(str.charAt(i) >= 'a' && str.charAt(i)<='z')
{
cCount++;
}
}
System.out.println("Number of vowels: " + vCount);
System.out.println("Number of consonants: " + cCount);
}
}