C Program to Count Vowels and Consonants.

Write a C program to count the number of vowels and consonants in a given string. Vowels are the letters 'a', 'e', 'i', 'o', and 'u' (both uppercase and lowercase), while consonants are all other letters of the alphabet.

Example:
Input: School
Output: Number of Vowels = 2
        Number of consonants = 4

Input: Programming
Output: Number of Vowels = 3
        Number of consonants = 8 

Vowel: A vowel is a type of speech sound produced by the vocal cords without any significant constriction or closure of the air passage. In the English alphabet, the letters 'a', 'e', 'i', 'o', and 'u' are considered vowels. (alert-success)
Consonant: A consonant is a speech sound in which the air is at least partially obstructed and does not flow freely through the vocal cords. In the English alphabet, all letters except 'a', 'e', 'i', 'o', and 'u' are considered consonants. (alert-success)


Step-by-step Algorithm.

Step 1: Input the string from the user.
Step 2: Initialize variables to store the counts of vowels and consonants, initially set to 0.
Step 3: Traverse each character of the string.
Step 4: Check if the character is an alphabet (a-z, A-Z).
Step 5: If the character is a vowel (either lowercase or uppercase), increment the vowel count.
Step 6: If the character is a consonant (neither a vowel nor a space), increment the consonant count.
Step 7: Display the final counts of vowels and consonants.


C code to count the number of vowels and consonants in the given string.

//C program to count vowels and consonants
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[100];
    int vowels = 0, consonants = 0, i = 0;

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

    while (str[i] != '\0') {
        // Convert character to lowercase
        char ch = tolower(str[i]); 

        // Check if the character is an alphabet
        if ((ch >= 'a' && ch <= 'z')) {
            // character is a vowel, increment the vowel count
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                vowels++;
            } else {
                // character is a consonant, increment the consonant count
                consonants++;
            }
        }
        i++;
    }

    printf("Number of vowels: %d\n", vowels);
    printf("Number of consonants: %d\n", consonants);

    return 0;
}
Output:
Enter a string: Good Life
Number of vowels: 4
Number of consonants: 4

Time Complexity: O(n) where n is the number of character the string contain.
Space Complexity: O(1) as no extra space is required to solve this problem.
Note: In the above code, we have used tolower() built-in function which is a part of ctype.h header file. It is used to convert characters to lowercase equivalents. (alert-passed) 

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS