0
0
CProgramBeginner · 2 min read

C Program to Count Vowels and Consonants

A C program to count vowels and consonants reads a string, then uses if conditions to check each character if it is a vowel or consonant, and increments counters accordingly.
📋

Examples

Inputhello
OutputVowels: 2 Consonants: 3
InputProgramming
OutputVowels: 3 Consonants: 8
Input12345
OutputVowels: 0 Consonants: 0
🧠

How to Think About It

To count vowels and consonants, first get the input string. Then look at each character one by one. If the character is a letter, check if it is a vowel by comparing it to 'a', 'e', 'i', 'o', 'u' (both lowercase and uppercase). If yes, add one to the vowel count. If it is a letter but not a vowel, add one to the consonant count. Ignore any other characters like numbers or symbols.
📐

Algorithm

1
Get input string from the user.
2
Initialize vowel and consonant counters to zero.
3
For each character in the string:
4
Check if it is an alphabet letter.
5
If it is a vowel (a, e, i, o, u), increment vowel counter.
6
Else if it is a consonant, increment consonant counter.
7
After checking all characters, print the counts.
💻

Code

c
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[100];
    int vowels = 0, consonants = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (int i = 0; str[i] != '\0'; i++) {
        char 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\nConsonants: %d\n", vowels, consonants);
    return 0;
}
Output
Enter a string: Programming Vowels: 3 Consonants: 8
🔍

Dry Run

Let's trace the input "hello" through the code

1

Input string

str = "hello"

2

Initialize counters

vowels = 0, consonants = 0

3

Check each character

h (consonant), e (vowel), l (consonant), l (consonant), o (vowel)

4

Final counts

vowels = 2, consonants = 3

CharacterLowercaseIs Letter?Vowel?Vowels CountConsonants Count
hhYesNo01
eeYesYes11
llYesNo12
llYesNo13
ooYesYes23
💡

Why This Works

Step 1: Convert to lowercase

Using tolower() makes checking vowels easier by ignoring case differences.

Step 2: Check if character is a letter

We only count vowels and consonants if the character is between 'a' and 'z'.

Step 3: Count vowels and consonants

If the letter matches any vowel, increment vowel count; otherwise, increment consonant count.

🔄

Alternative Approaches

Using switch-case
c
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[100];
    int vowels = 0, consonants = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (int i = 0; str[i] != '\0'; i++) {
        char ch = tolower(str[i]);
        if (ch >= 'a' && ch <= 'z') {
            switch(ch) {
                case 'a': case 'e': case 'i': case 'o': case 'u':
                    vowels++;
                    break;
                default:
                    consonants++;
            }
        }
    }

    printf("Vowels: %d\nConsonants: %d\n", vowels, consonants);
    return 0;
}
Switch-case can be clearer for vowel checking but is slightly longer.
Using isalpha() function
c
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[100];
    int vowels = 0, consonants = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (int i = 0; str[i] != '\0'; i++) {
        char ch = tolower(str[i]);
        if (isalpha(ch)) {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                vowels++;
            else
                consonants++;
        }
    }

    printf("Vowels: %d\nConsonants: %d\n", vowels, consonants);
    return 0;
}
Using <code>isalpha()</code> makes the code more readable and handles letters safely.

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 the input string are stored, so space is constant.

Which Approach is Fastest?

All approaches run in O(n) time; using isalpha() or switch-case mainly affects readability, not speed.

ApproachTimeSpaceBest For
If-else with tolowerO(n)O(1)Simple and clear code
Switch-caseO(n)O(1)Clear vowel checking, slightly longer code
Using isalpha()O(n)O(1)Better readability and safe letter check
💡
Use tolower() to simplify vowel checks by ignoring uppercase letters.
⚠️
Beginners often forget to check if characters are letters before counting, causing wrong counts.