C Program
#include <stdio.h> #include <ctype.h> int main() { char str[100]; int vowels = 0, consonants = 0; printf("Enter a string: "); gets(str); 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, Consonants = %d\n", vowels, consonants); return 0; }
C Output
Input: Hello World Output: Vowels = 3, Consonants = 7
C++ Program
#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 << ", Consonants = " << consonants << endl; return 0; }
C++ Output
Input: Programming Output: Vowels = 3, Consonants = 8
JAVA Program
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 (ch >= 'a' && ch <= 'z') { if ("aeiou".indexOf(ch) != -1) vowels++; else consonants++; } } System.out.println("Vowels = " + vowels + ", Consonants = " + consonants); } }
JAVA Output
Input: Java Language Output: Vowels = 6, Consonants = 7
Python Program
s = input("Enter a string: ").lower() vowels = consonants = 0 for ch in s: if ch.isalpha(): if ch in "aeiou": vowels += 1 else: consonants += 1 print(f"Vowels = {vowels}, Consonants = {consonants}")
Python Output
Input: Python Rocks Output: Vowels = 3, Consonants = 8
In-Depth Explanation
Example
When we input the string "Hello World", the program identifies each character individually. The vowels are e, o, and o, while H, l, l, W, r, l, and d are consonants. So, the program prints 3 vowels and 7 consonants. This is an easy operation that helps us analyze the text structure of words or sentences.
Real-Life Analogy
Imagine vowels and consonants as the "rhythm makers" and "sound shapers" of language. Vowels provide smooth flow to speech like the music in the background, while consonants provide structure, like beats. If a sentence contained no vowels, it would be stiff and unreadable like a robot. If it contained no consonants, it would be one big hum. In text-processing systems, it is as crucial to separate vowels and consonants as it is to balance ingredients in a recipe—too many of one and the flavor (or readability) is altered.
Why It Matters
This course introduces beginners to string traversal, character classification, and conditions of programming. Knowing how to recognize vowels and consonants is a base in natural language processing (NLP), spell-checkers, voice recognition, and even in game-like uses such as creating word games or speech synthesizers. It also refines the skill in working with loops, conditionals, and string manipulation, which are elementary programming building blocks.
Learning Insights
By doing this exercise, students master how to:
Use a string as a set of characters.
Effectively use conditional checks (if statements).
Work with built-in functions such as isalpha() and tolower() to write more readable code.
Count and categorize data, a concept used extensively in data analytics and AI.
Real-World Applications
Vowel and consonant counting may look like a tiny assignment, yet same reasoning is used in practical implementations like text-to-speech software, spell checkers, search engines parsing word frequency, and even educational implementations that enable children to learn phonetics. Same logic is used with data parsing and natural language comprehension in machine learning applications.
SEO-Optimized Closing Paragraph
Knowing how to program to count vowels and consonants in C, C++, Java, and Python is among the most essential programming exercises at the beginner level. It enhances problem-solving skills along with learning string manipulation, conditional statements, and looping. Whether one is preparing for coding interviews, solving MSBTE diploma problems, or creating real-world applications of text analysis, this program assists students in understanding basic concepts. Mastery of vowel and consonant counting programs enhances rational thinking, familiarizes students with manipulating characters from various programming languages, and lays a firm basis for higher-level concepts in natural language processing, data analysis, and algorithm construction.
Social Plugin