Thursday, August 28, 2025
HomeLanguagesJavaJava Program to Count the Total Number of Vowels and Consonants in...

Java Program to Count the Total Number of Vowels and Consonants in a String

Given a String count the total number of vowels and consonants in this given string. Assuming String may contain only special characters, or white spaces, or a combination of all. The idea is to iterate the string and checks if that character is present in the reference string or not. If a character is present in the reference increment number of vowels by 1, otherwise, increment the number of consonants by 1.

Example:

Input : String = "Lazyroar"
Output: Number of Vowels = 5
        Number of Consonants = 8

Input : String = "Alice"
Output: Number of Vowels = 3
        Number of Consonants = 2

Approach:

  1. Create two variables vow and cons and initialize them with 0.
  2. Start string traversing.
  3. If i’th character is vowel then increment in vow variable by 1.
  4. Else if the character is consonant then increment in cons variable by 1.

Example

Java




// Java Program to Count Total Number of Vowels
// and Consonants in a String
 
// Importing all utility classes
import java.util.*;
 
// Main class
class GFG {
   
     // Method 1
    // To prints number of vowels and consonants
    public static void count(String str)
    {
        // Initially initializing elements with zero
        // as till now we have not traversed 
        int vow = 0, con = 0;
       
        // Declaring a reference String
        // which contains all the vowels
        String ref = "aeiouAEIOU";
       
        for (int i = 0; i < str.length(); i++) {
             
            // Check for any special characters present
            // in the given string
            if ((str.charAt(i) >= 'A'
                 && str.charAt(i) <= 'Z')
                || (str.charAt(i) >= 'a'
                    && str.charAt(i) <= 'z')) {
                if (ref.indexOf(str.charAt(i)) != -1)
                    vow++;
                else
                    con++;
            }
        }
       
        // Print and display number of vowels and consonants
        // on console
        System.out.println("Number of Vowels = " + vow
                           + "\nNumber of Consonants = "
                           + con);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Custom string as input
        String str = "#Lazyroar";
       
        // Calling the method 1
        count(str);
    }
}


Output

Number of Vowels = 5
Number of Consonants = 8

Time Complexity: O(n²) here, n is the length of the string. 

RELATED ARTICLES

Most Popular

Dominic
32236 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6609 POSTS0 COMMENTS
Nicole Veronica
11779 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11828 POSTS0 COMMENTS
Shaida Kate Naidoo
6719 POSTS0 COMMENTS
Ted Musemwa
7002 POSTS0 COMMENTS
Thapelo Manthata
6678 POSTS0 COMMENTS
Umr Jansen
6690 POSTS0 COMMENTS