Check if a Character is a Vowel - C Program

Check if a Character is a Vowel - C Program

Check if a Character is a Vowel - C Program

This blog post demonstrates a simple C program to determine if a given character is a vowel or not. The program does not use any additional functions or libraries, making it straightforward and easy to understand.

Program Code

#include <stdio.h>

int main() {
    char character;

    // Prompt user for input
    printf("Enter a character: ");
    scanf(" %c", &character);

    // Convert character to lowercase manually
    if (character >= 'A' && character <= 'Z') {
        character = character + ('a' - 'A');
    }

    // Check if the character is a vowel
    if (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 not a vowel.\n", character);
    }

    return 0;
}

Explanation

This program performs the following steps:

  1. Include the Standard I/O Library: The program begins by including the <stdio.h> library for input and output functions.
  2. Read User Input: The program prompts the user to enter a single character and stores it in the character variable.
  3. Convert to Lowercase: It manually converts any uppercase character to lowercase by adjusting its ASCII value.
  4. Check for Vowel: The program then checks if the character is one of the vowels ('a', 'e', 'i', 'o', 'u').
  5. Output Result: It prints whether the character is a vowel or not based on the check.

This simple approach avoids using additional functions or libraries and is ideal for learning basic C programming concepts.

Comments

Popular Posts