Introduction
Counting vowels in a string is one of the most common beginner-level programming problems in Java. It helps developers understand important concepts such as strings, character arrays, loops, conditional statements, and string manipulation.
A vowel is a character that represents a basic sound in the English language. The five commonly used vowels are:
a, e, i, o, u
Both uppercase and lowercase vowels can appear in a string.
For example:
Input String:
Hello Java Programming
Vowels:
e, o, a, a, o, i
Total Number of Vowels:
6
Java provides multiple ways to count vowels in a string, such as using:
- For loop
- Character comparison
- Regular expressions
- Java Streams
In this tutorial, you will learn how to write a Java program to count vowels in a string, understand the algorithm, explore different approaches, analyze complexity, learn common mistakes, and prepare for interview questions.
What is Count Vowels Program?
The Count Vowels Program is a Java program that counts the total number of vowel characters present inside a given string.
The program checks each character of the string and determines whether it is:
- a
- e
- i
- o
- u
If the character matches any vowel, the counter value is increased by one.
Example:
String:
Programming
Characters:
P r o g r a m m i n g
Vowels:
o, a, i
Total Vowels:
3
Syntax
if(character == 'a' ||
character == 'e' ||
character == 'i' ||
character == 'o' ||
character == 'u') {
count++;
}
For uppercase and lowercase characters:
if(character == 'a' || character == 'A')
Java Count Vowels Program Using For Loop
public class CountVowels {
public static void main(String[] args) {
String text = "Hello Java Programming";
int count = 0;
for(int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' ||
ch == 'O' || ch == 'U') {
count++;
}
}
System.out.println("Total vowels: " + count);
}
}
Output
Explanation of the Program
Step 1: Create a String
String text = "Hello Java Programming";
This string contains characters that need to be checked.
Step 2: Create Counter Variable
int count = 0;
The variable stores the total number of vowels.
Initially:
count = 0
Step 3: Traverse the String
for(int i = 0; i < text.length(); i++)
The loop checks each character one by one.
Example:
H
e
l
l
o
Step 4: Get Each Character
char ch = text.charAt(i);
The charAt() method returns the character at a specific index.
Example:
text.charAt(1)
Output
Step 5: Check Vowel Condition
if(ch == 'a' || ch == 'e')
If the character is a vowel, increase the counter.
count++;
Java Count Vowels Program Using User Input
import java.util.Scanner;
public class CountVowels {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String text = scanner.nextLine();
int count = 0;
for(int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' ||
ch == 'O' || ch == 'U') {
count++;
}
}
System.out.println("Number of vowels: " + count);
scanner.close();
}
}
Output
Enter a string:
Number of vowels: 6
Java Count Vowels Using Character Array
Another approach is converting the string into a character array.
public class CountVowels {
public static void main(String[] args) {
String text = "Computer Science";
char[] characters = text.toCharArray();
int count = 0;
for(char ch : characters) {
if(ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' ||
ch == 'O' || ch == 'U') {
count++;
}
}
System.out.println("Total vowels: " + count);
}
}
Output
Java Count Vowels Using Regular Expression
Java also supports regular expressions for pattern matching.
public class CountVowels {
public static void main(String[] args) {
String text = "Java Programming";
int count = text.replaceAll("[^aeiouAEIOU]", "").length();
System.out.println("Total vowels: " + count);
}
}
Output
Algorithm
- Start the program.
- Read a string.
- Initialize vowel count to zero.
- Traverse each character of the string.
- Check whether the character is a vowel.
- If it is a vowel, increase the count.
- Display the total number of vowels.
- End the program.
Flowchart
Start
|
|
Read String
|
|
Initialize Count = 0
|
|
Read Each Character
|
|
Is Character a Vowel?
|
Yes No
| |
Count++ Continue
|
Next Character
|
Display Count
|
End
Time Complexity
The program checks every character of the string once.
If the string length is n:
Time Complexity = O(n)
Example:
For:
Hello Java
The program checks each character individually.
Space Complexity
Space Complexity = O(1)
Only one variable is used to store the count.
Advantages of Count Vowels Program
- Easy to understand and implement.
- Helps beginners learn string handling.
- Uses basic Java concepts.
- Works with any string length.
- Useful for text-processing applications.
Disadvantages of Count Vowels Program
- Requires checking every character.
- Performance decreases for very large strings.
- Basic approach does not handle all languages.
- Regular expressions may have additional overhead.
Common Mistakes
1. Checking Only Lowercase Vowels
Incorrect:
if(ch == 'a' || ch == 'e')
This ignores uppercase vowels.
Correct:
if(ch == 'a' || ch == 'A')
2. Forgetting String Length
Incorrect:
for(int i = 0; i <= text.length(); i++)
This causes:
StringIndexOutOfBoundsException
Correct:
for(int i = 0; i < text.length(); i++)
3. Comparing String Instead of Character
Incorrect:
if(ch.equals("a"))
Correct:
if(ch == 'a')
4. Forgetting to Increase Counter
Incorrect:
if(ch == 'a')
{
}
Correct:
count++;
5. Ignoring Empty String
Always handle empty input.
Example:
Input:
""
Output
Real-Life Applications
Counting vowels is useful in:
- Text analysis applications
- Word processing software
- Search engines
- Natural language processing
- Data validation systems
- Educational applications
- Chat applications
Examples:
- Analyzing sentence structure.
- Processing user input.
- Performing language analysis.
Summary
The Java Count Vowels Program is a simple but important beginner-level program that helps developers understand string manipulation and character processing.
By using loops and conditional statements, Java can easily identify vowel characters and count their occurrences in a string. This concept is useful in many real-world applications such as text processing, search systems, and natural language processing.