Java Program to count the total number of vowels and consonants in a string

In this program, our task is to count the total number of vowels and consonants present in the given string.

As we know that, the characters a, e, i, o, u are known as vowels in the English alphabet. Any character other than that is known as the consonant.

To solve this problem, First of all, we need to convert every upper-case character in the string to lower-case so that the comparisons can be done with the lower-case vowels only not upper-case vowels, i.e.(A, E, I, O, U). Then, we have to traverse the string using a for or while loop and match each character with all the vowels, i.e., a, e, i, o, u. If the match is found, increase the value of count by 1 otherwise continue with the normal flow of the program. The algorithm of the program is given below.

Algorithm

  • STEP 1: START
  • STEP 2: SET vCount =0, cCount =0
  • STEP 3: DEFINE string str = “This is a really simple sentence”.
  • STEP 4: CONVERT str to lowercase
  • STEP 5: SET i =0.
  • STEP 6: REPEAT STEP 6 to STEP 8 UNTIL i<str.length()
  • STEP 7: IF any character of str matches with any vowel then
    vCount = vCount + 1.
    STEP 8: IF any character excepting vowels lies BETWEEN a and z then
    cCount = cCount =+1.
  • STEP 9: i = i + 1
  • STEP 10: PRINT vCount.
  • STEP 11: PRINT cCount.
  • STEP 12: END

Program:

  1. public class CountVowelConsonant {    
  2.     public static void main(String[] args) {    
  3.             
  4.         //Counter variable to store the count of vowels and consonant    
  5.         int vCount = 0, cCount = 0;    
  6.             
  7.         //Declare a string    
  8.         String str = “This is a really simple sentence”;    
  9.             
  10.         //Converting entire string to lower case to reduce the comparisons    
  11.         str = str.toLowerCase();    
  12.             
  13.         for(int i = 0; i < str.length(); i++) {    
  14.             //Checks whether a character is a vowel    
  15.             if(str.charAt(i) == ‘a’ || str.charAt(i) == ‘e’ || str.charAt(i) == ‘i’ || str.charAt(i) == ‘o’ || str.charAt(i) == ‘u’) {    
  16.                 //Increments the vowel counter    
  17.                 vCount++;    
  18.             }    
  19.             //Checks whether a character is a consonant    
  20.             else if(str.charAt(i) >= ‘a’ && str.charAt(i)<=’z’) {      
  21.                 //Increments the consonant counter    
  22.                 cCount++;    
  23.             }    
  24.         }    
  25.         System.out.println(“Number of vowels: ” + vCount);    
  26.         System.out.println(“Number of consonants: ” + cCount);    
  27.     }    
  28. }   

Output:

Number of vowels: 10
Number of consonants: 17

Leave a Comment