Cranes at VIIT Q5

Count Vowels and Consonants in a String

Write a C program that counts the number of vowels and consonants in a given string.

Problem Statement:

Input Format:

Output Format:

Example Inputs & Outputs:

Enter the string: Hello World
Vowels: 3
Consonants: 7
            
Enter the string: C Programming
Vowels: 3
Consonants: 9
            

Constraints:

Hint:

To solve this problem, follow these steps:

  1. Iterate Through the String:
    • Use a loop to go through each character of the string.
    • Check if the character is a letter using the isalpha() function.
  2. Check for Vowels and Consonants:
    • Convert the character to lowercase using tolower() if necessary.
    • Check if the character is a vowel:
      if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                                  
    • If the character is not a vowel but is an alphabet, it is a consonant.
  3. Ignore Non-Alphabetic Characters:
    • Use the isalpha() function to check if the character is a letter.
  4. Print the Count:
    • Print the final count of vowels and consonants.

Formula Breakdown:

Task: Implement the above logic and write a C program to count the number of vowels and consonants in a string.