BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Count Vowels and Consonants in a String

Last Updated: April 10, 2019 by Chaitanya Singh | Filed Under: Java Examples

In this article, we will write a Java program to count the vowels and consonants in a String.

Program to count vowels and consonants in a given String

Here we have two variables vcount and ccount to keep the count of vowels and consonants respectively. We have converted each char of the string to lowercase using toLowerCase() method for easy comparison.

We are then comparing each char of the string to vowels ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ using charAt() method and if..else..if statement, if a match is found then we are increasing the vowel counter vcount else we are increasing the Consonant counter ccount.

public class JavaExample {

    public static void main(String[] args) {
        String str = "BeginnersBook";
        int vcount = 0, ccount = 0;

        //converting all the chars to lowercase
        str = str.toLowerCase();
        for(int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vcount++; } else if((ch >= 'a'&& ch <= 'z')) {
                ccount++;
            }
        }
        System.out.println("Number of Vowels: " + vcount);
        System.out.println("Number of Consonants: " + ccount);
    }
}

Output:
Java program to count vowels and consonants in a String

Related Java programs

1. Java program to find the occurrence of a character in a String
2. Java program to perform bubble sort on Strings
3. Java program to reverse a String
4. Java program to check vowel or consonant using Switch Case

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java program to perform Bubble Sort on Strings
  3. java program to find factorial of a given number using recursion
  4. Tech Number Program in Java
  5. How to convert a char array to a string in Java?

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap