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.
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.
In your terminal, navigate to the ~/project/
directory and create a new file called main.c
.
In the main.c
file, start by writing the program's boilerplate code.
#include <stdio.h>
int main() {
// Your code here
return 0;
}
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;
}
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;
}
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 (uppercase, lowercase, non-vowels) and make sure the program correctly identifies the vowels.
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.