Kodekraftt
/Blog
/Count Consonants 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 Consonants in a String in Java — Definition, Examples and Program

Learn how to count consonants in a string using Java. This blog explains the concept with examples, a simple Java program, and practice challenges for beginners.

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

Counting consonants in a string helps beginners understand how to work with characters, conditions, and loops in Java. A consonant is any alphabet character that is not a vowel.

In this blog, you will learn:

  • What consonants are
  • How to count consonants in a string
  • Step-by-step examples
  • A simple Java program
  • Practice challenges

What Are Consonants?

Consonants are all alphabet characters except vowels.

Vowels: a, e, i, o, u

Consonants: b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, y, z

How the Logic Works

To count consonants in a string:

  1. Read the input string.
  2. Traverse each character of the string.
  3. Check if the character is an alphabet.
  4. Ensure the character is not a vowel.
  5. Increase the consonant count.

Example 1

Input: "programming"

  • Vowels: o, a, i → 3
  • Total letters: 11
  • Consonants: 11 − 3 = 8

Total Consonants: 8

Example 2

Input: "HELLO WORLD"

  • Ignore space
  • Vowels: E, O, O → 3
  • Consonants: H, L, L, W, R, L, D → 7

Total Consonants: 7

Java Program to Count Consonants


import java.util.Scanner;

public class CountConsonants {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

        int consonantCount = 0;

        input = input.toLowerCase();

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

            if (ch >= 'a' && ch <= 'z') {
                if (ch != 'a' && ch != 'e' && ch != 'i' &&
                    ch != 'o' && ch != 'u') {
                    consonantCount++;
                }
            }
        }

        System.out.println("Number of consonants: " + consonantCount);
    }
}

Sample Output


Enter a string: Education
Number of consonants: 4

Practice Challenges

  1. Modify the program to count vowels, consonants, digits, and spaces separately.
  2. Count consonants without converting the string to lowercase.
  3. Find the most frequent consonant in a string.

Counting consonants strengthens your understanding of character classification and conditional logic in Java. It is a common interview problem and a great stepping stone to advanced string manipulation.

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.