C Program to Check Whether a Character is a Vowel or Consonant

Identifying whether a character is a vowel or consonant is a basic exercise in C programming that strengthens understanding of control statements and character manipulation. In this article we will expore how to write C Program to Check Whether a Character is a Vowel or Consonant using if-else statements, switch-case, and string handling each method is explained with code, output examples, and clear descriptions. You can also refer to our complete guide to learn C Program to Check Palindrome Number.

C Program to Check Whether a Character is a Vowel or Consonant: A Complete Guide

What is a Vowel or Consonant?

  • Vowels: The letters a, e, i, o, u (both uppercase and lowercase).
  • Consonants: All other alphabetic characters, excluding vowels.
    Non-alphabetic characters like digits and symbols are neither vowels nor consonants.

Method 1) C Program to Check Whether a Character is a Vowel or Consonant Using If-Else Statements

The if-else statement is a simple and widely used approach to check whether a character is a vowel or consonant.

Code Example

#include <stdio.h>

int main() {
    char c;
    printf("Enter a character: ");
    scanf("%c", &c);

    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
        c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
        printf("%c is a vowel.\n", c);
    } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
        printf("%c is a consonant.\n", c);
    } else {
        printf("%c is not an alphabet.\n", c);
    }

    return 0;
}

Explanation

  1. The program checks if the input character matches any vowel (both cases).
  2. If not, it verifies if the character falls within the alphabetic range (a-z or A-Z).
  3. Non-alphabetic inputs are identified and handled with a specific message.

Output Examples

Input: e
Output: e is a vowel.

Input: K
Output: K is a consonant.

Input: 9
Output: 9 is not an alphabet.

Method 2) C Program to Check Whether a Character is a Vowel or Consonant Using Switch-Case

The switch-case statement provides a structured and efficient way to determine vowels.

Code Example

#include <stdio.h>

int main() {
    char c;
    printf("Enter a character: ");
    scanf("%c", &c);

    switch (c) {
        case 'a': 
        case 'e': 
        case 'i': 
        case 'o': 
        case 'u': 
        case 'A': 
        case 'E': 
        case 'I': 
        case 'O': 
        case 'U':
            printf("%c is a vowel.\n", c);
            break;
        default:
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                printf("%c is a consonant.\n", c);
            } else {
                printf("%c is not an alphabet.\n", c);
            }
    }

    return 0;
}

Explanation

  1. The switch statement checks if the character matches any vowel case.
  2. The default block handles all other inputs, including consonants and non-alphabetic characters.

Output

Input: O
Output: O is a vowel.

Input: r
Output: r is a consonant.

Input: #
Output: # is not an alphabet.

Method 3) C Program to Check Whether a Character is a Vowel or Consonant Using String Handling

This approach uses string handling functions like strchr to check for vowels efficiently.

Code Example

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

int main() {
    char c;
    printf("Enter a character: ");
    scanf(" %c", &c); // Space before %c to handle whitespace

    char vowels[] = "aeiouAEIOU";

    if (strchr(vowels, c)) {
        printf("%c is a vowel.\n", c);
    } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
        printf("%c is a consonant.\n", c);
    } else {
        printf("%c is not an alphabet.\n", c);
    }

    return 0;
}

Explanation

  1. The strchr function checks if the input character exists in the vowels string.
  2. If not, the program checks whether the character is alphabetic and prints the appropriate message.

Output Examples

Input: i
Output: i is a vowel.

Input: g
Output: g is a consonant.

Input: 5
Output: 5 is not an alphabet.

Conclusion

These three methods illustrate different ways to determine if a character is a vowel or consonant in C. Each approach is suited for specific use cases:

  • If-else: Best for straightforward checks.
  • Switch-case: Ideal for fixed conditions.
  • String handling: Efficient for repetitive checks in larger applications.

Frequently Asked Questions (FAQs)

How to check if a character is a vowel or consonant in C?

You can use if-else statements, switch-case, or string functions like strchr to determine if a character is a vowel or consonant.

Can this program handle non-alphabetic characters?

Yes, all methods include checks to identify and handle non-alphabetic characters appropriately.

What is the best approach for beginners?

The if-else statement method is the simplest and easiest for beginners to understand.

Can I use strings to check multiple characters at once?

Yes, by iterating through each character in a string, you can check each one individually.

What are the vowels in C programming?

In C programming, vowels include the characters: a, e, i, o, u (both uppercase and lowercase).

Is this program case-sensitive?

No, the examples provided handle both uppercase and lowercase letters.

How can I optimize a vowel or consonant check in C?

Using switch-case or pre-defined strings with functions like strchr can make the program more efficient and concise.

Can I write a function to check vowels and consonants?

Yes, creating separate functions like isVowel and isConsonant can modularize your code and improve readability.

How do I extend the program for strings?

Loop through each character of the string and apply the same logic to determine vowels and consonants.

Does this work for Unicode characters?

The examples provided are designed for standard ASCII characters. For Unicode, you’ll need additional libraries and wide-character functions.

By understanding these methods and FAQs, you can write robust programs to determine whether a character is a vowel or consonant while improving your C programming skills.

Categories C

Leave a Comment