Kodekraftt
/Blog
/Count Vowels In A String In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

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 vowels are
  • How to count vowels in a string
  • Step-by-step examples
  • A simple Java program
  • Practice challenges

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"

  • p → not a vowel
  • r → not a vowel
  • o → vowel (count = 1)
  • a → vowel (count = 2)
  • i → vowel (count = 3)

Total Vowels: 3

Example 2

Input: "HELLO WORLD"

  • E → vowel
  • O → vowel
  • O → vowel

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.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.