Skip to content
doubtly

1 answer

Answer: read the string, then check each character and increase the matching counter.

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

int main() {
    char str[200];
    int i, vowels = 0, consonants = 0, digits = 0, spaces = 0;
    char ch;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (i = 0; str[i] != '\0'; i++) {
        ch = tolower(str[i]);
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
            vowels++;
        else if (ch >= 'a' && ch <= 'z')
            consonants++;
        else if (ch >= '0' && ch <= '9')
            digits++;
        else if (ch == ' ')
            spaces++;
    }

    printf("Vowels: %d\nConsonants: %d\n", vowels, consonants);
    printf("Digits: %d\nBlank spaces: %d\n", digits, spaces);
    return 0;
}

Explanation: fgets reads the whole line including spaces (scanf("%s") would stop at the first space). tolower lets one check cover both cases. A letter that is not a vowel is a consonant; other characters (punctuation, the newline) are not counted.

Sample: "Hello World 2024" gives vowels 3, consonants 7, digits 4, blank spaces 2.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.