The Count Vowels and Consonants program in C is designed to count and display the number of vowels and consonants in a given string. This program typically involves iterating through each character of the string, checking whether it's a vowel or a consonant, and maintaining a count of each.

Implementation of Count Vowels and Consonants Program

Here’s a simple implementation of this program in C:

#include <stdio.h> #include <ctype.h> int main() { char str[100]; // Array to hold the input string int vowels = 0; // Counter for vowels int consonants = 0; // Counter for consonants int i = 0; // Loop counter // Ask user for input printf("Enter a string: "); fgets(str, sizeof(str), stdin); // Read the input string including spaces // Iterate through each character in the string while (str[i] != '\0') { // Convert character to lowercase for uniform comparison char ch = tolower(str[i]); // Check if the character is a letter if (isalpha(ch)) { // Check if the character is a vowel if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowels++; // Increment vowel count } else { consonants++; // Increment consonant count } } i++; // Move to the next character } // Display the results printf("Vowels: %d\n", vowels); printf("Consonants: %d\n", consonants); return 0; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: For standard input and output functions.
    • #include <ctype.h>: For character handling functions like tolower() and isalpha().
  2. Variable Declarations:

    • char str[100]: An array to hold the input string with a maximum length of 99 characters (the last character is reserved for the null terminator).
    • int vowels = 0: Counter for vowels, initialized to 0.
    • int consonants = 0: Counter for consonants, initialized to 0.
    • int i = 0: Loop counter for iterating through the string.
  3. User Input:

    • The program prompts the user to enter a string, which is read using fgets(). This function allows spaces in the input.
  4. Character Iteration:

    • A while loop is used to iterate through each character of the string until the null terminator ('\0') is reached.
  5. Character Processing:

    • Each character is converted to lowercase using tolower() for uniform comparison.
    • The isalpha() function checks if the character is a letter.
    • If the character is a vowel (checked against 'a', 'e', 'i', 'o', 'u'), the vowel counter is incremented.
    • If it is not a vowel but is an alphabetic character, the consonant counter is incremented.
  6. Display Results:

    • After iterating through the string, the program prints the total counts of vowels and consonants.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc count_vowels_consonants.c -o count_vowels_consonants
  2. Execute the Program:

    ./count_vowels_consonants
  3. Input Data: Enter a string when prompted.

Example Input/Output

Input:

Enter a string: Hello World!

Output:

Vowels: 3 Consonants: 7

Conclusion

The Count Vowels and Consonants program in C is a practical exercise that demonstrates how to manipulate strings and utilize character handling functions. It reinforces the understanding of loops, conditionals, and basic string operations in C programming. This type of program can be easily extended or modified to include additional functionality, such as counting specific letters or ignoring certain characters.