Java Program to Count Vowels and Consonants in the String

count vowel and consonant in string java

In this tutorial, we are going to learn java programming to count the number of vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) and consonants (any alphabet other than vowels) in a string.

For any input string, we have to count the number of vowels that appear and the number of consonants that appear. And returns the number of vowels and consonants as an output of our program.

For example

Case1: For the input string ‘Python Is Fun’

The output should be the number of vowels that is 3(‘o’, ‘I’, ‘u’) and the number of consonants that is 8(‘P’, ‘y’, ‘t’, ‘h’, ‘n’, ‘s’, ‘F’, ‘n’).

Case 2: For the input string ‘Quescol’

The output should be the number of vowels that is 3(‘u’, ‘e’, ‘o’) and the number of consonants that is 4(‘Q’, ‘s’, ‘c’, ‘l’).

Java Program to Count Vowels and Consonants in the String

Output

java program to count vowel and consonant

Explanation

For the input string ‘Quescol website’, the string is first iterated through each of each character, and as the vowels ‘u’, ‘e’,‘i’, and ‘o’ are found the variable ‘vowel’ is incremented each time the vowels are found, In this case, the ‘vowel’ variable is incremented 6 times from ‘0’.

Similarly, the variable ‘consonants’ will increment itself each time it encounters a consonant. In this case, the consonant will increment itself 8 times as it found consonants ‘Q’, ‘s’, ‘c’, ‘l’, ‘w’, ‘b’, ‘s’ and ‘t’.