✓ Link copied to clipboard
C 📅 July 11, 2026 ⏱ 5 min read

C Program to Count Vowels and Consonants in a String

C Program to Count Vowels and Consonants in a String

Ever thought about how to count vowels and consonants in a string using C? Let’s break it down. We’ll read a string from the user and tally up the vowels and consonants it has. For instance, if you put in "abcde", you'll find out there are 2 vowels and 3 consonants. Let’s dive into the details.

Prerequisites for the C Program to Count Vowels and Consonants

Before writing this program, ensure you have:

  • Basic understanding of strings in C
  • Familiarity with loops and conditional statements
  • Knowledge of the <stdio.h> and <string.h> header files
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Count Vowels and Consonants in a String

Here's the complete code. It reads input, processes each character, and counts the vowels and consonants.

count_vowels_consonants.cC▶ Run
#include <stdio.h>
#include <string.h>

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

    // Input a string from the user
    printf("Enter a string: ");
    fgets(str, 100, stdin);

    // Iterate through each character of the string
    for(i = 0; i < strlen(str); i++) {
        // Check if the character is a vowel
        if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
            str[i] == 'o' || str[i] == 'u' || str[i] == 'A' ||
            str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
            vowels++;
        }
        // Check if the character is a consonant
        else if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
            consonants++;
        }
    }

    // Print the count of vowels and consonants
    printf("Vowels: %d\n", vowels);
    printf("Consonants: %d\n", consonants);
    
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Count Vowels and Consonants Program

Once you run the program and enter a string like "abcde", you'll see the results.

OUTPUTtext
Enter a string: abcde
Vowels: 2
Consonants: 3
Output of C Program to Count Vowels and Consonants in a String
Output of C Program to Count Vowels and Consonants in a String

Algorithm to Count Vowels and Consonants in C

Counting vowels and consonants involves checking each character and categorizing it. Here's how the program logically processes the string.

  1. Read the input string from the user.
  2. Initialize the vowel and consonant counters to zero.
  3. Iterate through each character of the string.
  4. Check if the character is a vowel. If yes, increment the vowel counter.
  5. If it's an alphabet but not a vowel, increment the consonant counter.
  6. Continue until all characters are processed.
  7. Output the counts of vowels and consonants.
flowchart TD
  A([Start]) --> B[/Enter a string/]
  B --> C[vowel = 0, consonant = 0]
  C --> D{i < strlen(str)?}
  D -->|Yes| E[Check if str[i] is a vowel]
  E --> F{Is Vowel?}
  F -->|Yes| G[vowel++]
  F -->|No| H{Is Alphabet?}
  H -->|Yes| I[consonant++]
  H -->|No| J[i++]
  G --> J
  I --> J
  J --> D
  D -->|No| K([End])
Flowchart: the algorithm step by step

How the C Program to Count Vowels and Consonants Works

Let's break down the code and understand each part's function.

  • The #include <stdio.h> and #include <string.h> headers are necessary for input/output functions and string manipulation.
  • We declare a character array str to store the user's input and two integer variables vowels and consonants to count each.
  • fgets(str, 100, stdin) reads the input string including spaces.
  • The for loop iterates over each character of str using strlen(str) to determine its length.
  • Inside the loop, an if statement checks if a character is a vowel. If true, it increments vowels.
  • The else if checks if a character is a consonant, incrementing consonants if true.
  • Finally, printf functions print the results.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What will be the output if the input string is 'abcde'?

🎉Correct! The vowels are 'a' and 'e', and the consonants are 'b', 'c', 'd'.
🤔Not quite. Count the vowels and consonants carefully.

Time and Space Complexity of the C Program

Analyzing the performance of our program is crucial.

Time Complexity: O(n) — n is the number of characters in the string, like 5 for "abcde". Each character is checked once, making the process linear. For instance, if you input a 10-character string, it makes 10 checks.

Space Complexity: O(1) — Only a few variables like vowels and consonants are used, regardless of the string length.

⚠️

Common mistake: Not checking for both lowercase and uppercase vowels can mess up your counts.

Key takeaway: This program highlights character processing, a vital skill in text analysis.

FAQs About Counting Vowels and Consonants in C

Here are some common questions about this program:

Yep, it uses fgets which reads spaces too.
fgets is great for strings with spaces.
Nope, the program considers both as vowels.
Digits don’t count in vowel and consonant tally.
Yes, just change the if conditions to check for digits.
The program sticks to a fixed number of variables for counting.

If you found this program helpful, check out these related programs:

Conclusion

Counting vowels and consonants is a basic yet important task when dealing with text data. The logic here sets you up for more complex stuff like text analysis or natural language processing. If you found this interesting, try expanding the program to include other character types! Keep practicing to sharpen your skills.

Test your string manipulation skills with the C language quiz. Run the program in the EduSeekho compiler to see it work!