Guide on C Program to Determine Whether a Character is a Vowel or Consonant

Checking whether a character is a vowel or consonant is a fundamental exercise in C programming. This article will cover various methods to perform this check. We will explore multiple C Program to Determine Whether a Character is a Vowel or Consonant, each demonstrating a different approach to solve the problem. Additionally, we will discuss the prerequisites, provide detailed explanations for each example, and conclude with a summary of what we’ve learned.

Prerequisites

Before we delve into the examples, ensure you have the following prerequisites:

  • A C compiler (such as GCC)
  • A text editor or IDE for writing your C code
  • Basic understanding of C programming concepts, particularly conditionals and character handling

1. Identifying Vowels and Consonants in C

In this section, we will look at several methods to determine whether a character is a vowel or consonant in C.

1.1 Using Simple if-else Statements

This method uses basic if-else statements to check if the character is a vowel or consonant.

Code

C
#include <stdio.h>

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch);

    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
        printf("%c is a vowel.\n", ch);
    } else {
        printf("%c is a consonant.\n", ch);
    }

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the character: scanf("%c", &ch) reads a character from the user.
  • Check for vowels: An if statement checks if the character is a vowel.
  • Output the result: printf displays whether the character is a vowel or consonant.

Output

C
Enter a character: a
a is a vowel.

1.2 Using switch Statement

This method uses a switch statement to check if the character is a vowel or consonant.

Code

C
#include <stdio.h>

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch);

    switch(ch) {
        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", ch);
            break;
        default:
            printf("%c is a consonant.\n", ch);
    }

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the character: scanf("%c", &ch) reads a character from the user.
  • Check for vowels using switch: The switch statement checks if the character is a vowel.
  • Output the result: printf displays whether the character is a vowel or consonant.

Output

C
Enter a character: E
E is a vowel.

1.3 Using Functions to check vowel or consonant in c

This method encapsulates the vowel/consonant check in a separate function for better modularity.

Code

C
#include <stdio.h>

int isVowel(char ch);

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch);

    if (isVowel(ch)) {
        printf("%c is a vowel.\n", ch);
    } else {
        printf("%c is a consonant.\n", ch);
    }

    return 0;
}

int isVowel(char ch) {
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
        return 1;
    } else {
        return 0;
    }
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Declare a function: int isVowel(char ch) checks if a character is a vowel.
  • Input the character and call the function: scanf("%c", &ch) reads the character and isVowel(ch) checks if it is a vowel.
  • Output the result: printf displays whether the character is a vowel or consonant.

Output

C
Enter a character: o
o is a vowel.

1.4 Using ctype.h Library

This method utilizes the ctype.h library for character handling functions like tolower and check vowel or consonant.

Code

C
#include <stdio.h>
#include <ctype.h>

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch);

    ch = tolower(ch);

    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
        printf("%c is a vowel.\n", ch);
    } else {
        printf("%c is a consonant.\n", ch);
    }

    return 0;
}

Explanation

  • Include necessary headers: #include <stdio.h> for input/output functions and #include <ctype.h> for character functions.
  • Input the character: scanf("%c", &ch) reads a character from the user.
  • Convert to lowercase: ch = tolower(ch) converts the character to lowercase.
  • Check for vowels: An if statement checks if the character is a vowel.
  • Output the result: printf displays whether the character is a vowel or consonant.

Output

C
Enter a character: U
u is a vowel.

2. Conclusion

In this article, we explored several C Program to Determine Whether a Character is a Vowel or Consonant using simple if-else statements, using a switch statement, using a function, and using the ctype.h library. Each method demonstrates different aspects of handling character input and making decisions in C programming. By understanding these methods, you can choose the one that best fits your specific needs and enhance your skills in conditional statements and function usage in C.