C PROGRAMMING PROGRAM • STRINGS
Count Vowels, Consonants, Digits and Spaces
Learn how to count vowels, consonants, digits and spaces using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
C is fun 2026
Sample output
Vowels = 2 Consonants = 4 Digits = 4 Spaces = 3
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char text[300];
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
fgets(text, sizeof text, stdin);
for (int index = 0; text[index] != '\0'; index++) {
unsigned char value = (unsigned char) text[index];
if (isdigit(value)) digits++;
else if (isspace(value)) { if (value != '\n') spaces++; }
else if (isalpha(value)) {
char lower = (char) tolower(value);
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') vowels++;
else consonants++;
}
}
printf("Vowels = %d\nConsonants = %d\nDigits = %d\nSpaces = %d\n", vowels, consonants, digits, spaces);
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Vowels = 2 Consonants = 4 Digits = 4 Spaces = 3
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Classify each character with ctype functions and update the matching counter.
- Display the computed result.
Classify each character with ctype functions and update the matching counter.
EFFICIENCY
Time and space complexity
Time complexity
O(n)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
