0
0
CsharpProgramBeginner · 2 min read

C# Program to Count Vowels and Consonants

Use a C# program that loops through each character of a string, checks if it is a vowel with "aeiou".Contains(char.ToLower(c)), counts vowels and consonants separately, and prints the totals.
📋

Examples

Inputhello
OutputVowels: 2, Consonants: 3
InputProgramming
OutputVowels: 3, Consonants: 8
Input123!@#
OutputVowels: 0, Consonants: 0
🧠

How to Think About It

To count vowels and consonants, read each character in the input string. Convert it to lowercase to simplify checks. If the character is a letter, check if it is one of the vowels (a, e, i, o, u). If yes, increase the vowel count; if not, increase the consonant count. Ignore non-letter characters.
📐

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 a letter.
5
If it is a vowel, increment vowel count; else increment consonant count.
6
Print the counts of vowels and consonants.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();
        int vowels = 0, consonants = 0;
        foreach (char c in input) {
            if (char.IsLetter(c)) {
                char lower = char.ToLower(c);
                if ("aeiou".Contains(lower)) vowels++;
                else consonants++;
            }
        }
        Console.WriteLine($"Vowels: {vowels}, Consonants: {consonants}");
    }
}
Output
Enter a string: Programming Vowels: 3, Consonants: 8
🔍

Dry Run

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

1

Input received

input = "hello"

2

Initialize counters

vowels = 0, consonants = 0

3

Check each character

h (letter, not vowel) -> consonants = 1 e (vowel) -> vowels = 1 l (consonant) -> consonants = 2 l (consonant) -> consonants = 3 o (vowel) -> vowels = 2

4

Print result

Vowels: 2, Consonants: 3

CharacterIsLetterIsVowelVowels CountConsonants Count
htruefalse01
etruetrue11
ltruefalse12
ltruefalse13
otruetrue23
💡

Why This Works

Step 1: Check if character is a letter

We use char.IsLetter(c) to ignore digits and symbols, counting only alphabetic characters.

Step 2: Convert to lowercase

Converting to lowercase with char.ToLower(c) simplifies vowel checking by avoiding case mismatches.

Step 3: Count vowels and consonants

If the character is in "aeiou", increment vowel count; otherwise, increment consonant count.

🔄

Alternative Approaches

Using LINQ to count vowels and consonants
csharp
using System;
using System.Linq;
class Program {
    static void Main() {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();
        int vowels = input.Count(c => "aeiouAEIOU".Contains(c));
        int consonants = input.Count(c => char.IsLetter(c) && !"aeiouAEIOU".Contains(c));
        Console.WriteLine($"Vowels: {vowels}, Consonants: {consonants}");
    }
}
This approach is concise and uses LINQ but may be less clear for beginners.
Using switch statement for vowel checking
csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();
        int vowels = 0, consonants = 0;
        foreach (char c in input) {
            if (char.IsLetter(c)) {
                switch (char.ToLower(c)) {
                    case 'a': case 'e': case 'i': case 'o': case 'u': vowels++; break;
                    default: consonants++; break;
                }
            }
        }
        Console.WriteLine($"Vowels: {vowels}, Consonants: {consonants}");
    }
}
Using switch can be clearer for some learners but is more verbose.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through each character once, so time grows linearly with input size.

Space Complexity

Only a few counters are used, so extra space is constant regardless of input size.

Which Approach is Fastest?

The basic loop is efficient and clear; LINQ is concise but may have slight overhead.

ApproachTimeSpaceBest For
Basic loop with ContainsO(n)O(1)Clarity and beginner-friendly
LINQ Count with lambdaO(n)O(1)Concise code, intermediate users
Switch statementO(n)O(1)Explicit vowel checks, readability
💡
Always convert characters to lowercase before checking vowels to simplify your conditions.
⚠️
Counting non-letter characters as vowels or consonants instead of ignoring them.