c program to print count of consonants, vowels and digits

In this example, we take string as input and display count of vowels, consonants, digits and white space.

Count the Number of Vowels, Consonants, Digits In C Programming

	
#include <stdio.h>
int main() {
    char line[150];
    int vowels, consonant, digit, space;

    vowels = consonant = digit = space = 0;

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

    for (int i = 0; line[i] != '\0'; ++i) {
        if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
            line[i] == 'o' || line[i] == 'u' || line[i] == 'A' ||
            line[i] == 'E' || line[i] == 'I' || line[i] == 'O' ||
            line[i] == 'U') {
            vowels++;
        } else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {
            consonant++;
        } else if (line[i] >= '0' && line[i] <= '9') {
            digit++;
        } else if (line[i] == ' ') {
            space++;
        }
    }

    printf("Vowels: %d", vowels);
    printf("\nConsonants: %d", consonant);
    printf("\nDigits: %d", digit);
    printf("\nWhite spaces: %d", space);
    return 0;
}
	

Output :

	
Enter a line of string: a quick brown fox jumps over the lazy dog
Vowels: 11
Consonants: 22
Digits: 0
White spaces: 8
	

Share your thoughts

Ask anything about this examples