C Program to Count the Number of Vowels, Consonants, and Other Characters

Counting the number of vowels, consonants, digits, spaces, and special characters in a string is a common task in text processing. This article presents three different C Program to Count the Number of Vowels, Consonants, and Other Characters, complete with explanations, example code, and outputs.

Prerequisites

Before diving into the solutions, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with arrays, strings, and loops.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Using a Simple Loop

1.1 Explanation

In this approach, we use a simple loop to iterate through each character in the string and count the number of vowels, consonants, digits, spaces, and special characters.

1.2 Program

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

void countCharacters(char *str) {
    int vowels = 0, consonants = 0, digits = 0, spaces = 0, special = 0;

    for (int i = 0; str[i] != '\0'; i++) {
        char 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++;
        } else {
            special++;
        }
    }

    printf("Vowels: %d\n", vowels);
    printf("Consonants: %d\n", consonants);
    printf("Digits: %d\n", digits);
    printf("Spaces: %d\n", spaces);
    printf("Special characters: %d\n", special);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    countCharacters(str);
    return 0;
}

1.3 Output

C
Enter a string: Hello World! 123
Vowels: 3
Consonants: 7
Digits: 3
Spaces: 2
Special characters: 1

Solution 2: Using Arrays

2.1 Explanation

This approach uses arrays to store the counts of vowels, consonants, digits, spaces, and special characters, providing a more organized way to manage the counts.

2.2 Program

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

void countCharacters(char *str) {
    int counts[5] = {0}; // 0: vowels, 1: consonants, 2: digits, 3: spaces, 4: special

    for (int i = 0; i < strlen(str); i++) {
        char ch = tolower(str[i]);
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            counts[0]++;
        } else if (ch >= 'a' && ch <= 'z') {
            counts[1]++;
        } else if (ch >= '0' && ch <= '9') {
            counts[2]++;
        } else if (ch == ' ') {
            counts[3]++;
        } else {
            counts[4]++;
        }
    }

    printf("Vowels: %d\n", counts[0]);
    printf("Consonants: %d\n", counts[1]);
    printf("Digits: %d\n", counts[2]);
    printf("Spaces: %d\n", counts[3]);
    printf("Special characters: %d\n", counts[4]);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    countCharacters(str);
    return 0;
}

Output

C
Enter a string: C Programming 101!
Vowels: 4
Consonants: 9
Digits: 3
Spaces: 2
Special characters: 1

Solution 3: Using Pointers

3.1 Explanation

In this approach, we use pointers to traverse the string and count the number of vowels, consonants, digits, spaces, and special characters, which can be more efficient in certain scenarios.

3.2 Program

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

void countCharacters(char *str) {
    int vowels = 0, consonants = 0, digits = 0, spaces = 0, special = 0;

    while (*str) {
        char ch = tolower(*str);
        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++;
        } else {
            special++;
        }
        str++;
    }

    printf("Vowels: %d\n", vowels);
    printf("Consonants: %d\n", consonants);
    printf("Digits: %d\n", digits);
    printf("Spaces: %d\n", spaces);
    printf("Special characters: %d\n", special);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    countCharacters(str);
    return 0;
}

Output

C
Enter a string: Learn C Programming!
Vowels: 5
Consonants: 9
Digits: 0
Spaces: 2
Special characters: 2

Conclusion

Counting the number of vowels, consonants, digits, spaces, and special characters in a string is a fundamental task in text processing. This article presented three different methods to achieve this in C programming: using a simple loop, using arrays, and using pointers. Each method has its advantages and can be chosen based on the specific requirements and constraints of your application. By understanding and experimenting with these techniques, you can enhance your text processing capabilities and improve your overall programming skills in C.