Count Vowels, Consonants in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

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

int main() {
    char s[100]; int v=0, c=0;
    gets(s);
    for(int i=0; s[i]; i++) {
        char ch = tolower(s[i]);
        if(ch >= 'a' && ch <= 'z')
            (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') ? v++ : c++;
    }
    printf("Vowels=%d Consonants=%d", v, c);
}

C Output

Input:  
Hello World  

Output:  
Vowels=3 Consonants=7


C++ Program

#include<iostream>
using namespace std;

int main() {
    string s; getline(cin, s);
    int v=0, c=0;
    for(char ch : s) {
        ch = tolower(ch);
        if(ch >= 'a' && ch <= 'z')
            (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') ? v++ : c++;
    }
    cout << "Vowels=" << v << " Consonants=" << c;
}

C++ Output

Input:  
Programming is fun  

Output:  
Vowels=5 Consonants=11


JAVA Program

import java.util.*;

class VC {
    public static void main(String[] a) {
        String s = new Scanner(System.in).nextLine();
        int v=0, c=0;
        for(char ch : s.toLowerCase().toCharArray())
            if(ch >= 'a' && ch <= 'z')
                if("aeiou".indexOf(ch) >= 0) v++; else c++;
        System.out.println("Vowels=" + v + " Consonants=" + c);
    }
}

JAVA Output

Input:  
Java is Cool  

Output:  
Vowels=5 Consonants=5


Python Program

s = input().lower()
v = sum(1 for ch in s if ch in 'aeiou')
c = sum(1 for ch in s if ch.isalpha() and ch not in 'aeiou')
print("Vowels=", v, "Consonants=", c)

Python Output

Input:  
Python is Amazing  

Output:  
Vowels=5 Consonants=9


In-Depth Learning – Full Concept in Paragraphs
What Are Vowels and Consonants in Programming?
When we talk about vowels in programming, we are talking about the characters of the English alphabet: a, e, i, o, u (either in lowercase or uppercase). The consonants are all other letters from a through z except vowels. Spaces, digits, and punctuation are not counted when calculating vowels and consonants.

This problem illustrates how to:

Loop through a string

Check character conditions

Use pattern matching or logical conditions

How the Code Functions
Every program takes the string input and normalizes all the characters to lowercase for consistent comparison. Then, for every character:

If it is a letter (between 'a' and 'z')

See if it's a vowel: count the vowels

Else, it's a consonant: count consonants

Characters such as spaces, punctuation, or numbers are skipped by verifying if the character lies between the alphabet range or by using isalpha().

Python employs generators for counting, whereas C and Java employ conventional for loops. C++ employs the contemporary for(char ch : s) loop for aesthetics.

Example
Suppose input is:

nginx
Copy
Edit
Hello World
Lowercase version: hello world
Then:

Vowels: e, o, o → 3 vowels

Consonants: h, l, l, w, r, l, d → 7 consonants

Output:

ini

Vowels=3 Consonants=7
Real-Life Analogy
Imagine vowels as voice sounds and consonants as mouth sounds in speech. Vowels are musical sounds in any sentence — just as in coding, they form part of what makes the "sound" or shape of a word. Vowel and consonant counting is similar to breaking down the rhythm of a language or grasping patterns in text, commonly applied to language processing problems.

Where and When Is It Used?
This exercise is helpful in:

Natural Language Processing (NLP)

Preparing for interviews

String handling exercises for beginners

Spell checking, phonetic spelling, pattern matching

Speech synthesis or AI voice interaction

It's a building block for text analysis and character-level logic learning in programming.

Time and Space Complexity
Time Complexity: O(n) — You need to traverse every character once.

Space Complexity: O(1) — Few counters are required.

Python's list comprehensions or sum() with generators are fast and space-efficient.

Python-Specific Benefit
Python’s expressive power allows for elegant counting:


sum(1 for ch in s if ch in 'aeiou')
This is powerful for text-heavy applications and lets beginners quickly write meaningful logic in minimal code.

SEO-Optimized Natural Paragraph for Ranking
Want to count vowels and consonants in a string using C, C++, Java, or Python? This guide offers the shortest and most accurate code to count how many vowels and consonants are in any given text. This reasoning is fundamental to string manipulation, text analysis, and programming basics learning for novices. The idea enhances comprehension of loops, character comparison, and parsing of text. As a student, an interview candidate, or a programming hobbyist, understanding this easy yet strong program initiates the gateway to complex language processing and algorithm creation.