C Program to Count Vowels, Consonants, Digits and Spaces in Given String

Write a program in c to find frequency of Vowel, Consonant, Digits and Spaces in a String

Given a string, we have to count the number of vowels, consonants, digits and white space characters in it and print the counts on screen. English has five proper vowel letters (A, E, I, O, U) and all alphabets except these characters are consonants. There are 10 digits in decimal number systems, from '0' to '9' and in this program we will only check for space character ' ' and not other white space characters like tab and new line.

C program to count the number or vowels, consonants, digits and spaces in a string

In this program, we first take a string as input from user using gets function. We are using four integer variables V, C, D and W as counters for Vowels, Consonants, Digits and Space characters. Here, we are using user defined functions to check for various characters as follows :

  • int isVowel(char c) : Returns 1 if passed character is vowel, otherwise 0.
  • int isConsonant(char c) : Returns 1 if passed character is consonant, otherwise 0.
  • int isDigit(char c) : Returns 1 if passed character is digit, otherwise 0.
  • int isWhitespace(char c) : Returns 1 if passed character is space, otherwise 0.

Using a for loop we traverse input string from index 0 till '\0' character and check every character using above mentioned four functions.

* C program to count number of Vowel, Consonant, 
* Digits and Spaces in a string

int isVowel(char c);
int isConsonant(char c);
int isDigit(char c);
int isWhitespace(char c);

int main(){
    char str[500];
    int V = 0, C = 0, D = 0, W = 0, i;
    printf("Enter a string\n");
    for(i = 0;str[i] != '\0'; i++) {
        V += isVowel(str[i]);
        C += isConsonant(str[i]);
        D += isDigit(str[i]);
        W += isWhitespace(str[i]);
    printf("Vowels: %d\n",V);
    printf("Consonants: %d\n",C);
    printf("Digits: %d\n",D);
    printf("White spaces: %d",W);
 return 0;

int isVowel(char c){
     return 1;
 } else {
     return 0;

int isConsonant(char c) {
 if(((c>='a'&& c<='z') || (c>='A'&& c<='Z')) && !isVowel(c)){
  return 1;
 } else {
  return 0;

int isDigit(char c) {
  return 1;
 } else {
  return 0;

int isWhitespace(char c) {
 if(c == ' '){
  return 1;
 } else {
  return 0;
Program Output
Enter a string
C is my 1st programming language
Vowels: 8
Consonants: 18 
Digits: 1
White spaces: 5

Related Topics
C Program to find frequency of characters in a string
C program to check whether an alphabet is a vowel or consonant
C program to remove extra spaces from string
C program to remove vowels from a string
C program to compare two strings
C program to check string is palindrome
C program to sort characters of a string
List of all C programs