Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Vowels Program in Java

Last Updated on April 26, 2023 by Prepbytes

Being able to manipulate strings is a fundamental skill that every programmer should have. Vowels are often used to test a programmer’s ability to manipulate strings. In this article, we will discuss how to write a vowels program in Java. We will provide a step-by-step algorithm for writing the vowels program in Java and code implementation in Java.

Understanding the Vowels Program in Java

The objective of this program is to determine the number of vowels and consonants within a given string. The vowels in the English alphabet are represented by the characters ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, with all other characters classified as consonants.

To address this problem, we begin by converting all uppercase letters in the string to lowercase, enabling the program to perform comparisons with only lowercase vowels (i.e., ‘a’, ‘e’, ‘i’, ‘o’, ‘u’) and not uppercase vowels (i.e., ‘A’, ‘E’, ‘I’, ‘O’, ‘U’). We then traverse the string using a for or while loop, comparing each character with all the vowels in turn. If a match is found, the program increases the count of vowels by 1; otherwise, the program continues with the regular flow of execution. The algorithm of the vowels program is outlined below.

Algorithm for Vowels Program in Java

Below mentioned are the steps involved in the algorithm for the vowels program in Java.

  • Step 1 – Take a string as input from the user.
  • Step 2 – Convert the input string to lowercase.
  • Step 3 – Initialize the countVowels variable, to 0.
  • Step 4 – Traverse the input string character by character using a FOR loop.
  • Step 5 – Check if the current character is a vowel (i.e., ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’). If it is, increment the countVowels variable by 1.
  • Step 6 – After the loop is completed, print the count of vowels present in the input string.

Implementation of Vowels Program in Java

Here’s the Java code implementation for counting the number of vowels in a given string:

Code Implementation:

import java.util.Scanner;

class PrepBytes {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(;
        String inputString = scanner.nextLine().toLowerCase();
        int vowelCount = 0;
        for (int i = 0; i < inputString.length(); i++) {
            char currentChar = inputString.charAt(i);

            if (currentChar == 'a' || currentChar == 'e' || currentChar == 'i' || currentChar == 'o' || currentChar == 'u') {
        System.out.println("Number of vowels: " + vowelCount);


Hello, Welcome to PrepBytes!!


Number of vowels: 8

In conclusion, we discussed the vowels program in Java to count the number of vowels in a given string. We reviewed the algorithm for solving this problem, which involves traversing the string and comparing each character with the vowels in the English alphabet. We also saw the Java code implementation of this program, which uses a for loop and conditional statements to determine the number of vowels in the input string. This program can be useful in various applications such as natural language processing, text analysis, and data mining.

FAQs on Vowels Program in Java

Here are some frequently asked questions related to vowels program in Java.

Q1: How do you determine if a character is a vowel in Java?
Answer: In Java, characters can be compared using the "==" operator. To determine if a character is a vowel, we compare it with the vowels in the English alphabet, i.e., ‘a’, ‘e’, ‘i’, ‘o’, ‘u’.

Q2: What happens if the input string contains non-alphabetical characters?
Answer: If the input string contains non-alphabetical characters, the program will ignore them and count only the alphabetical characters as vowels and consonants.

Q3: How does the program handle spaces in the input string?
Answer: The program treats spaces as non-alphabetical characters and ignores them while counting the number of vowels and consonants.

Q4: What is the time complexity of the program?
Answer: The time complexity of the program is O(n), where n is the length of the input string.

Q5: Can the program count the number of vowels and consonants in non-English languages?
Answer: No, the program can only count the number of vowels and consonants in the English language since it only checks for the vowels in the English alphabet.

Q6: Can the program count the number of punctuation marks in a string?
Answer: No, the program is designed to count only the number of vowels and consonants in a string and does not consider punctuation marks as alphabetical characters.

Q7: How can the program be used in data mining?
Answer: The program can be used in data mining to preprocess textual data and extract useful features such as the frequency of vowels and consonants in a document or a set of documents.

Leave a Reply

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