You are currently viewing Check a Character is Vowel Or Consonant Or A Special Character Using If-else in C

Check a Character is Vowel Or Consonant Or A Special Character Using If-else in C

In this tutorial, you will learn how to write a program to check whether a character is a vowel, constant, or special character by using if-else decision-making aspects. We are explaining this problem with examples and algorithms here.

Problem Statement

Write a C program that checks whether a given character is a vowel, consonant, or special character.

Example

Enter a character: A
Output: The character A is a vowel.

Enter a character: $
Output: The character $ is a special character.

Enter a character: 7
Output: The character 7 is a special character.

Algorithm to Check a Character is Vowel Or Consonant Or A Special Character

  1. Start the program.
  2. Declare a variable character of type char to store the input character.
  3. Prompt the user to enter a character by printing the message “Enter a character: “.
  4. Read the input character from the user and store it in the character variable using scanf().
  5. Use an if-else statement to check the category of the input character:
    • If character is equal to ‘A’, ‘E’, ‘I’, ‘O’, or ‘U’ (uppercase) or ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’ (lowercase), print the message “The character [character] is a vowel.”
    • If character is between ‘A’ and ‘Z’ (uppercase) or between ‘a’ and ‘z’ (lowercase), but not a vowel, print the message “The character [character] is a consonant.”
    • Otherwise, print the message “The character [character] is a special character.”
  6. End the program.

Program to check whether a character is a vowel or consonant or a special character

#include <stdio.h>
int main() {
    char character;
    printf("Enter a character: ");
    scanf(" %c", &character);
    if ((character >= 'A' && character <= 'Z') || (character >= 'a' && character <= 'z')) {
        if (character == 'A' || character == 'E' || character == 'I' || character == 'O' || character == 'U' ||
            character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u') {
            printf("The character %c is a vowel.\n", character);
        }
        else {
            printf("The character %c is a consonant.\n", character);
        }
    }
    else {
        printf("The character %c is a special character.\n", character);
    }
    return 0;
}

Output 1

Enter a character: w
The character w is a consonant

Output 2

Enter a character: E
The character E is a vowel

Output 3

Enter a character: @
The character @ is a special character

Program Explanation

  • The program starts by including the necessary header file stdio.h.
  • The main() function is defined.
  • Inside the main() function, a variable character of type char is declared to store the input character.
  • The program prompts the user to enter a character by printing the message “Enter a character: “.
  • The scanf() function is used to read a character from the user, and the input is stored in the character variable using the %c format specifier. A space is added before %c in scanf to consume any leading whitespace or newline characters from the previous input.
  • An if-else statement is used to check the category of the input character:
    • The first condition (character >= 'A' && character <= 'Z') || (character >= 'a' && character <= 'z') checks if the character is an alphabet.
    • If the character is an alphabet, it further checks if the character is a vowel or a consonant.
    • If the character is a vowel (‘A’, ‘E’, ‘I’, ‘O’, ‘U’, ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’), it prints the message “The character [character] is a vowel.” The %c format specifier is used in printf() to print the character.
    • If the character is an alphabet but not a vowel, it prints the message “The character [character] is a consonant.”
    • If the character is not an alphabet, it prints the message “The character [character] is a special character.”
  • The return 0 statement ends the program.

Conclusion

This C program allows the user to enter a character and determines whether it is a vowel, consonant, or a special character using if-else statements. It covers all possible cases and provides the appropriate output based on the input character.