C# Strings  

Count vowels and consonants in a String

Counting vowels and consonants in a string is a simple yet fundamental string manipulation problem that helps beginners understand character operations, conditional statements, and loops — essential skills for Data Structures and Algorithms (DSA).

Let’s explore what vowels and consonants are, understand the logic behind counting them, and then dive into the code examples.

🔠 What Are Vowels and Consonants?

In the English alphabet:

  • Vowels: A, E, I, O, U (and their lowercase forms a, e, i, o, u)

  • Consonants: All other alphabetic letters except vowels.

So, when we iterate through a string, we simply need to:

  1. Check if the character is a letter.

  2. If it’s a vowel, increase the vowel count.

  3. If it’s a consonant, increase the consonant count.

🧠 Logic / Algorithm

Here’s the step-by-step logic to count vowels and consonants:

  1. Initialize counters:

    • vowelCount = 0

    • consonantCount = 0

  2. Read the input string.

  3. Traverse each character in the string.

  4. Check if it’s an alphabet letter using conditions or ASCII checks.

  5. If it’s a vowel, increment vowelCount.

  6. If it’s a consonant, increment consonantCount.

  7. Display both counts at the end.

💻 C Program: Count Vowels and Consonants

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

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

    printf("Enter a string: ");
    gets(str); // avoid in modern C; use fgets instead

    for (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\n", vowels);
    printf("Consonants: %d\n", consonants);

    return 0;
}

⚙️ C++ Program: Count Vowels and Consonants

#include <iostream>
#include <cctype>
using namespace std;

int main() {
    string str;
    int vowels = 0, consonants = 0;

    cout << "Enter a string: ";
    getline(cin, str);

    for (char ch : str) {
        ch = tolower(ch);
        if (isalpha(ch)) {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                vowels++;
            else
                consonants++;
        }
    }

    cout << "Vowels: " << vowels << endl;
    cout << "Consonants: " << consonants << endl;

    return 0;
}

☕ Java Program: Count Vowels and Consonants

import java.util.Scanner;

public class CountVowelsConsonants {
    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 (Character.isLetter(ch)) {
                if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                    vowels++;
                else
                    consonants++;
            }
        }

        System.out.println("Vowels: " + vowels);
        System.out.println("Consonants: " + consonants);
    }
}

🧩 Example Output

Input

Hello World

Output

Vowels: 3  
Consonants: 7

🔍 Key Takeaways

  • Always check if the character is a letter before counting.

  • Handle both uppercase and lowercase letters using tolower() or toLowerCase().

  • Avoid deprecated functions like gets() in modern C.

  • This simple problem helps in understanding loops, conditional statements, and character handling — building blocks for more complex DSA problems.

🏁 Conclusion

Counting vowels and consonants in a string may look simple, but it’s a perfect example to strengthen your basics in programming. Once you’re confident with this, you can move on to more complex string manipulation problems like reversing words, finding palindromes, or removing duplicates.