Vowel Identification Using Switch Case

CCBeginner
Practice Now

Introduction

In C programming, a character is considered a vowel if it is 'a', 'e', 'i', 'o', or 'u', uppercase or lowercase. In this lab, you will learn how to write a program to check if an input character is a vowel or not, using Switch Case.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/switch("`Switch`") c/ControlFlowGroup -.-> c/break_continue("`Break/Continue`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123217{{"`Vowel Identification Using Switch Case`"}} c/comments -.-> lab-123217{{"`Vowel Identification Using Switch Case`"}} c/data_types -.-> lab-123217{{"`Vowel Identification Using Switch Case`"}} c/operators -.-> lab-123217{{"`Vowel Identification Using Switch Case`"}} c/switch -.-> lab-123217{{"`Vowel Identification Using Switch Case`"}} c/break_continue -.-> lab-123217{{"`Vowel Identification Using Switch Case`"}} c/user_input -.-> lab-123217{{"`Vowel Identification Using Switch Case`"}} c/memory_address -.-> lab-123217{{"`Vowel Identification Using Switch Case`"}} c/function_parameters -.-> lab-123217{{"`Vowel Identification Using Switch Case`"}} c/function_declaration -.-> lab-123217{{"`Vowel Identification Using Switch Case`"}} end

Create a new C file

In your terminal, navigate to the ~/project/ directory and create a new file called main.c.

Write the program's boilerplate code

In the main.c file, start by writing the program's boilerplate code.

#include <stdio.h>

int main() {
    // Your code here
    return 0;
}

Get user input

Ask the user to input a character to be checked by the program.

#include <stdio.h>

int main() {
    char ch;

    printf("Input a Character: ");
    scanf("%c", &ch);

    // Your code here
    return 0;
}

Check if the input is a vowel using Switch Case

With the user input stored in the variable ch, it's time to check if the input is a vowel using Switch Case.

#include <stdio.h>

int main() {
    char ch;
    printf("Input a Character: ");
    scanf("%c", &ch);

    switch(ch) {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            printf("%c is a vowel.\n", ch);
            break;
        default:
            printf("%c is not a vowel.\n", ch);
    }

    // Your code here
    return 0;
}

Run the program

Compile and run the program. Input a character when prompted and check if the program correctly identifies if it is a vowel or not.

Test the program with different inputs

Test the program with different inputs (uppercase, lowercase, non-vowels) and make sure the program correctly identifies the vowels.

Summary

In this lab, you learned how to write a C program to check if a character is a vowel using Switch Case. You also learned the importance of using break statements in each case to avoid executing unintended code and ensure efficient decision-making in your program.

Other C Tutorials you may like