Write a C program to check whether an alphabet is vowel or consonant using switch case
Question
Write a C program to check whether an alphabet is vowel or consonant using switch case
Solution
Here is a simple C program that checks whether the entered alphabet is a vowel or a consonant using a switch case statement:
#include <stdio.h>
int main() {
char alphabet;
// Prompt the user to enter an alphabet
printf("Enter an alphabet: ");
scanf("%c", &alphabet);
// Check if the input is a letter
if ((alphabet >= 'a' && alphabet <= 'z') || (alphabet >= 'A' && alphabet <= 'Z')) {
// Convert to lowercase for easier comparison
switch (alphabet) {
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", alphabet);
break;
default:
printf("%c is a consonant.\n", alphabet);
break;
}
} else {
printf("Invalid input. Please enter a valid alphabet.\n");
}
return 0;
}
Explanation:
- The program prompts the user for an input character.
- It checks whether the input character is a letter (either uppercase or lowercase).
- It uses a switch statement to determine if the letter is a vowel (a, e, i, o, u) or a consonant.
- It prints the result accordingly. If the input is not a valid letter, it notifies the user of the invalid input.
You can compile and run this program in a C programming environment.
Similar Questions
Write a C program to read an English Alphabet through keyboard and display whetherthe given alphabet is in upper case or lower case.
Write a program to check whether a given character is Alphabet or not using if else statement Note: Check for both upper and lower case characters
Write a C program to read a sentence and replace lowercase characters with uppercase and vice versa.
Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.
#Write a Python function count_vowels(text) that takes a string as input and returns the count of vowels (a, e, i, o, u) in the input string, ignoring case.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.