Count the Vowels and Consonants in a String
Count how many characters of a string are vowels and how many are consonants.
Walk through the string one character at a time. Lowercase each letter, and if it is a, e, i, o or u add it to the vowel count; if it is any other letter add it to the consonant count. Ignore spaces, digits and punctuation. One pass gives both totals in O(n) time.
Problem Statement
Given a string, count how many of its characters are vowels (a, e, i,
o, u) and how many are consonants. The check should be case-insensitive,
so A and a both count as vowels.
Ignore any character that is not an English letter, such as spaces, digits and punctuation. Report the two counts.
Input: A single string s.
Output: The vowel count and the consonant count.
Examples
Input: "hello"
Output: Vowels: 2, Consonants: 3e and o are vowels; h, l, l are consonants.
Input: "Java Programming"
Output: Vowels: 5, Consonants: 10Vowels a, a, o, a, i; the space is skipped and ten letters remain as consonants.
Constraints
0 <= s.length <= 10^5Only English letters a-z and A-Z are counted
Think Before You Code
Reveal the questions to ask yourself first
- How do you test whether a single character is a vowel?
- How do you make the check work for both uppercase and lowercase letters?
- What should happen to spaces, digits and punctuation?
- Do you need two separate counters or can you derive one from the other?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Make one pass over the string, classifying each character.
- Start
vowelsandconsonantsat 0. - For each character, convert it to lowercase.
- If it is not in the range
a–z, skip it (not a letter). - If it equals
a,e,i,ooru, incrementvowels. - Otherwise it is a consonant, so increment
consonants. - After the loop, report both counts.
Lowercasing first collapses the upper- and lowercase cases into one check, and the letter-range test keeps spaces and punctuation out of the totals.
Dry Run
Walk through the example step by step
Counting in "hello":
char | lower | letter? | vowel? | vowels | consonants
-----+-------+---------+--------+--------+-----------
h | h | yes | no | 0 | 1
e | e | yes | yes | 1 | 1
l | l | yes | no | 1 | 2
l | l | yes | no | 1 | 3
o | o | yes | yes | 2 | 3
final: Vowels: 2, Consonants: 3
Solution
Reveal the full Java solution
public class VowelConsonantCounter {
public static String count(String s) {
int vowels = 0;
int consonants = 0;
for (int i = 0; i < s.length(); i++) {
char c = Character.toLowerCase(s.charAt(i));
if (c < 'a' || c > 'z') {
continue; // skip spaces, digits, punctuation
}
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
vowels++;
} else {
consonants++;
}
}
return "Vowels: " + vowels + ", Consonants: " + consonants;
}
public static void main(String[] args) {
System.out.println(count("hello")); // Vowels: 2, Consonants: 3
System.out.println(count("Java Programming")); // Vowels: 5, Consonants: 10
}
}
Converting each character to lowercase means the vowel test only lists the five
lowercase vowels while still catching uppercase input. The range check
c < 'a' || c > 'z' filters out everything that is not a letter, so spaces and
symbols never inflate the consonant count. Everything happens in a single pass.
O(n) where n is the string lengthSpace: O(1)Common Mistakes
- Counting spaces or punctuation as consonants by skipping the letter check.
- Only checking lowercase vowels and missing uppercase A, E, I, O, U.
- Treating 'y' as a vowel without deciding on that rule first.
Edge Cases to Test
- An empty string yields Vowels: 0, Consonants: 0.
- A string of only spaces or digits yields both counts at 0.
- A string of only vowels like "aeiou" gives 5 vowels and 0 consonants.
Interview Follow-Ups
- How would you also count digits and spaces separately in the same pass?
- How would you return the results as a map instead of a formatted string?
- How would you extend the check to treat 'y' as a vowel?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →