C program to count the number of vowels, consonants and so on

This program counts the number of vowels, consonants, digits and white-spaces in a string which is entered by the user.

To understand this example, you should have the knowledge of following C programming topics:

Example: Program to count vowels, consonants etc.

#include <stdio.h>

int main()
    char line[150];
    int i, vowels, consonants, digits, spaces;

    vowels =  consonants = digits = spaces = 0;

    printf("Enter a line of string: ");
    scanf("%[^\n]", line);

    for(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' ||
        else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
        else if(line[i]>='0' && line[i]<='9')
        else if (line[i]==' ')

    printf("Vowels: %d",vowels);
    printf("\nConsonants: %d",consonants);
    printf("\nDigits: %d",digits);
    printf("\nWhite spaces: %d", spaces);

    return 0;


Enter a line of string: adfslkj34 34lkj343 34lk
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2

This program takes string input from the user and stores in variable line.

Initially, the variables vowels, consonants, digits and spaces are initialized to 0.

When the vowel character is found, vowel variable is incremented by 1. Similarly, consonants, digits and spaces are incremented when these characters are found.

Finally, the count is displayed on the screen.