Count Vowels in a String: flowchart, algorithm and program
This program reads a line of text and counts how many vowels and how many consonants it contains. It walks through the string one character at a time, converts each character to lowercase, and checks whether it is a letter and then whether it is a vowel.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Set vowels = 0, consonants = 0.
- Step 3: Read string str.
- Step 4: Set i = 0.
- Step 5: If str[i] != '\0', go to step 6; otherwise go to step 12.
- Step 6: Set ch = tolower(str[i]).
- Step 7: If ch is a letter (a-z), go to step 8; otherwise go to step 11.
- Step 8: If ch is a, e, i, o or u, go to step 9; otherwise go to step 10.
- Step 9: Increase vowels by 1. Go to step 11.
- Step 10: Increase consonants by 1.
- Step 11: Increase i by 1. Go to step 5.
- Step 12: Print vowels.
- Step 13: Print consonants.
- Step 14: Stop.
Pseudocode
BEGIN
vowels ← 0, consonants ← 0
READ string str
i ← 0
WHILE str[i] != '\0' DO
ch ← tolower(str[i])
IF ch is a letter (a-z) THEN
IF ch is a, e, i, o or u THEN
vowels++
ELSE
consonants++
END IF
END IF
i++
END WHILE
PRINT vowels
PRINT consonants
END
Program in C, Python and Java
Count Vowels in a String program in C
#include <stdio.h>
#include <ctype.h>
int main() {
char str[200];
int i, vowels = 0, consonants = 0;
char ch;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
ch = tolower(str[i]);
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowels++;
else
consonants++;
}
}
printf("Vowels = %d\n", vowels);
printf("Consonants = %d\n", consonants);
return 0;
}Count Vowels in a String program in Python
str1 = input("Enter a string: ")
vowels = 0
consonants = 0
for ch in str1.lower():
if ch >= 'a' and ch <= 'z':
if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u':
vowels += 1
else:
consonants += 1
print("Vowels =", vowels)
print("Consonants =", consonants)Count Vowels in a String program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int vowels = 0, consonants = 0;
System.out.print("Enter a string: ");
String str = sc.nextLine().toLowerCase();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowels++;
else
consonants++;
}
}
System.out.println("Vowels = " + vowels);
System.out.println("Consonants = " + consonants);
}
}
Sample input: Hello World
Output: Enter a string: (full run would then print) Vowels = 3 / Consonants = 7
How it works
- fgets() reads the whole line into str, spaces included, and the for loop runs from i = 0 until it reaches the '\0' that ends the string.
- Each character is converted with tolower(str[i]) and stored in ch, so 'A' and 'a' are treated the same and only five vowel checks are needed.
- The check ch >= 'a' && ch <= 'z' keeps only letters. Spaces, digits, punctuation and the newline that fgets keeps are skipped and counted as neither vowels nor consonants.
- Inside that check, if ch is a, e, i, o or u then vowels++ runs, and every other letter does consonants++.
- After the loop ends, both counters are printed. For "Hello World" the vowels are e, o, o (3) and the consonants are H, l, l, W, r, l, d (7).
Dry run (trace table)
| Step | Line | Box | vowels | consonants | str | i | ch | Output |
|---|---|---|---|---|---|---|---|---|
| 1 | 4 | Start | ||||||
| 2 | 6 | vowels = 0, consonants = 0 | 0 | 0 | ||||
| 3 | 10 | Read string str | 0 | 0 | "Hello World" | Enter a string: | ||
| 4 | 12 | i = 0 | 0 | 0 | "Hello World" | 0 | ||
| 5 | 12 | str[i] != '\0'? | 0 | 0 | 0 | |||
| 6 | 13 | ch = tolower(str[i]) | 0 | 0 | 0 | h | ||
| 7 | 14 | ch is a letter (a-z)? | 0 | 0 | 0 | h | ||
| 8 | 15 | ch is a, e, i, o or u? | 0 | 0 | 0 | h | ||
| 9 | 18 | consonants++ | 0 | 1 | 0 | h | ||
| 10 | 12 | i++ | 0 | 1 | 1 | h | ||
| 11 | 12 | str[i] != '\0'? | 0 | 1 | 1 | h | ||
| 12 | 13 | ch = tolower(str[i]) | 0 | 1 | 1 | e | ||
| 13 | 14 | ch is a letter (a-z)? | 0 | 1 | 1 | e | ||
| 14 | 15 | ch is a, e, i, o or u? | 0 | 1 | 1 | e | ||
| 15 | 16 | vowels++ | 1 | 1 | 1 | e | ||
| 16 | 12 | i++ | 1 | 1 | 2 | e | ||
| 17 | 12 | str[i] != '\0'? | 1 | 1 | 2 | e | ||
| 18 | 13 | ch = tolower(str[i]) | 1 | 1 | 2 | l | ||
| 19 | 14 | ch is a letter (a-z)? | 1 | 1 | 2 | l | ||
| 20 | 15 | ch is a, e, i, o or u? | 1 | 1 | 2 | l |
Time and space complexity
Time: O(n) · Space: O(1). The loop visits each of the n characters once and does constant work per character.
Common mistakes
- Counting every non-vowel as a consonant. Without the letter check, spaces, digits, punctuation and the trailing newline would be added to consonants and give a wrong total.
- Forgetting to convert to lowercase (or to check uppercase vowels too), so 'A', 'E' and so on are counted as consonants.
- Not initialising vowels and consonants to 0 before the loop, so they start with garbage values.
Viva questions
Q1. Why are vowels and consonants both initialised to 0?
They are counters that only ever increase with ++. If they started with a garbage value, the final counts would be wrong. Starting at 0 means 'nothing counted yet'.
Q2. Why does the loop condition check str[i] != '\0' instead of using a fixed length?
A C string ends with the null character '\0', so the loop runs until the end of whatever the user typed. The input can be shorter than the 200-character array, so a fixed length would read past the text.
Q3. What is the purpose of tolower(str[i]) on line 13?
It converts uppercase letters to lowercase so that 'A' and 'a' are handled the same way. This lets us compare against only 'a','e','i','o','u' instead of ten letters.
Q4. What does the condition ch >= 'a' && ch <= 'z' check, and why is it needed?
It checks that the character is an alphabet letter. Without it, spaces, digits, punctuation and the newline from fgets would be counted as consonants.
Q5. What happens if the input is an empty string, or contains only digits and spaces like "123 45"?
The loop either doesn't run or never enters the letter check, so both counts stay 0 and the program prints Vowels = 0 and Consonants = 0. There is no error.
Q6. Why use fgets instead of scanf("%s") here, and what does it do with the newline?
scanf("%s") stops at the first space, so only the first word would be read. fgets reads the whole line including spaces and limits input to the buffer size, which prevents overflow. It also stores the '\n', but that is ignored because it isn't a letter.
Q7. What is the time complexity of this program, and why?
O(n), where n is the length of the string. The loop visits each character exactly once and does constant work per character. The extra space is O(1), apart from the input buffer.
Q8. What would you change to also count digits and special characters, or to treat 'y' as a vowel?
For digits and special characters, add counters and an else branch for non-letters, using isdigit() or a plain else. For 'y', add ch == 'y' to the vowel condition. The rest of the logic stays the same.
Frequently asked questions
Why do we use tolower() in the vowel counting program?
It turns every letter into lowercase so you only compare against a, e, i, o, u. Without it you would need to check both 'a' and 'A' and so on for every vowel.
Why are spaces not counted as consonants?
A space is not a letter, so it fails the ch >= 'a' && ch <= 'z' test. Only characters that pass this test go on to the vowel or consonant counting.
Why use fgets() instead of scanf() or gets()?
fgets() reads a full line including spaces and will not write past the size of the array, which makes it safe. scanf("%s") stops at the first space, and gets() is unsafe and has been removed from modern C.