Java Program to Count Vowels and Consonants
if ("aeiou".indexOf(Character.toLowerCase(ch)) != -1), and counts vowels and consonants accordingly.Examples
How to Think About It
Algorithm
Code
import java.util.Scanner; public class VowelConsonantCounter { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a string: "); String input = scanner.nextLine(); int vowels = 0, consonants = 0; for (char ch : input.toCharArray()) { char lower = Character.toLowerCase(ch); if (Character.isLetter(lower)) { if ("aeiou".indexOf(lower) != -1) { vowels++; } else { consonants++; } } } System.out.println("Vowels: " + vowels); System.out.println("Consonants: " + consonants); scanner.close(); } }
Dry Run
Let's trace the input "hello" through the code
Input received
input = "hello"
Initialize counters
vowels = 0, consonants = 0
Check character 'h'
lower = 'h', isLetter = true, vowel check = false, consonants = 1
Check character 'e'
lower = 'e', isLetter = true, vowel check = true, vowels = 1
Check character 'l'
lower = 'l', isLetter = true, vowel check = false, consonants = 2
Check character 'l'
lower = 'l', isLetter = true, vowel check = false, consonants = 3
Check character 'o'
lower = 'o', isLetter = true, vowel check = true, vowels = 2
Print results
Vowels: 2, Consonants: 3
| Character | Is Letter | Is Vowel | Vowels Count | Consonants Count |
|---|---|---|---|---|
| h | true | false | 0 | 1 |
| e | true | true | 1 | 1 |
| l | true | false | 1 | 2 |
| l | true | false | 1 | 3 |
| o | true | true | 2 | 3 |
Why This Works
Step 1: Check each character
The program looks at each character in the string one by one to decide if it is a vowel or consonant.
Step 2: Use lowercase for easy comparison
Converting characters to lowercase helps to check vowels without worrying about uppercase letters.
Step 3: Count vowels and consonants
If the character is a letter and found in the vowel list, increase vowel count; otherwise, increase consonant count.
Alternative Approaches
import java.util.Scanner; public class VowelConsonantCounterSwitch { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a string: "); String input = scanner.nextLine(); int vowels = 0, consonants = 0; for (char ch : input.toCharArray()) { char lower = Character.toLowerCase(ch); if (Character.isLetter(lower)) { switch (lower) { case 'a': case 'e': case 'i': case 'o': case 'u': vowels++; break; default: consonants++; } } } System.out.println("Vowels: " + vowels); System.out.println("Consonants: " + consonants); scanner.close(); } }
import java.util.Scanner; public class VowelConsonantCounterRegex { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a string: "); String input = scanner.nextLine().toLowerCase().replaceAll("[^a-z]", ""); int vowels = 0, consonants = 0; for (char ch : input.toCharArray()) { if ("aeiou".indexOf(ch) != -1) { vowels++; } else { consonants++; } } System.out.println("Vowels: " + vowels); System.out.println("Consonants: " + consonants); scanner.close(); } }
Complexity: O(n) time, O(1) space
Time Complexity
The program checks each character once, so time grows linearly with input size.
Space Complexity
Only a few counters and variables are used, so space is constant regardless of input size.
Which Approach is Fastest?
The direct character check with indexOf is fast and uses minimal memory compared to regex filtering.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direct character check with indexOf | O(n) | O(1) | Simple and efficient for all inputs |
| Switch-case vowel check | O(n) | O(1) | Clearer logic but more code |
| Regex filtering then count | O(n) | O(n) | When you want to clean input first |