Count Vowels in a String in Java — Definition, Examples and Program

Learn how to count vowels in a string using Java. This blog explains the logic with step-by-step examples, a simple Java program, and practice challenges for beginners.

Count Vowels in a String in Java — Definition, Examples and Program

Counting vowels in a string is a basic string problem that helps beginners understand character comparison, loops, and conditional statements.

In this blog, you will learn:

What Are Vowels?

Vowels in the English alphabet are:

a, e, i, o, u

Both uppercase and lowercase vowels should be considered while counting.

How the Logic Works

To count vowels in a string:

  1. Read the string from the user.
  2. Traverse each character of the string.
  3. Check if the character is a vowel.
  4. Increase the count when a vowel is found.

Example 1

Input: "programming"

Total Vowels: 3

Example 2

Input: "HELLO WORLD"

Total Vowels: 3

Java Program to Count Vowels


import java.util.Scanner;

public class CountVowels {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String input = sc.nextLine();

        int vowelCount = 0;

        input = input.toLowerCase();

        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);

            if (ch == 'a' || ch == 'e' || ch == 'i' ||
                ch == 'o' || ch == 'u') {
                vowelCount++;
            }
        }

        System.out.println("Number of vowels: " + vowelCount);
    }
}

Sample Output


Enter a string: Education
Number of vowels: 5

Practice Challenges

  1. Modify the program to count consonants as well.
  2. Count vowels without converting the string to lowercase.
  3. Find which vowel appears the most times in a string.

Counting vowels is a simple yet powerful exercise that strengthens your understanding of strings and conditions in Java.