C PROGRAMMING PROGRAM • STRINGS

Count Vowels, Consonants, Digits and Spaces

Learn how to count vowels, consonants, digits and spaces using a clear C program.

Beginnerctype.hCharacter classesCounters

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

count-string-character-types.c
Open in compiler
#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;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

Vowels = 2
Consonants = 4
Digits = 4
Spaces = 3
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Classify each character with ctype functions and update the matching counter.
  3. 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.