English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of C Programming Examples
In this example, you will learn to check if the letters entered by the user are vowels or consonants.
To understand this example, you should understand the followingC programmingTopic:
These five letters A, E, I, O, and U are called vowels. Except for these5All other letters except the vowels are called consonants.
This program assumes that the user will always input an alphabet character.
#include <stdio.h> int main() { char c; int lowercase, uppercase; printf("Input alphabet: "); scanf("%c", &c); //If the variable c is lowercase, the value is calculated as1 lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); // If the variable c is uppercase, the value is calculated as1 uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); // If c is lowercase or uppercase, the value is calculated as1 if (lowercase || uppercase) printf("%c is a vowel.", c); else printf("%c is a consonant", c); return 0; }
Output result
Input alphabet: G G is a consonant
The character input by the user is stored in the variable c.
If c is a lowercase vowel, the calculation result of the lowercase variable is1(true), and for any other character, the calculation result is 0 (false).
Similarly, if the uppercase variable c is a uppercase vowel, then the value of the variable is1(true), and for any other character, the value of the variable is 0 (false).
If either lowercase or uppercase variable is1(true), then the input character is a vowel.
However, if both lowercase and uppercase variables are 0, the input character is a consonant.
Note:This program assumes that the user will input an alphabet. If the user inputs a non-alphabet character, the character is displayed as a constant.
To solve this problem, we can useisalpha()The function islapha() checks whether a character is an alphabet.
#include <stdio.h> #include <ctype.h> int main() { char c; int lowercase, uppercase; printf("Enter an alphabet: "); scanf("%c", &c); //If the variable c is lowercase, the value is calculated as1 lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); //If the variable c is uppercase, the calculation is1 uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); //If c is not an alphabet, an error message is displayed if (!isalpha(c)) { printf("Error! Non-alphabet character."); } // if c is an alphabet else { //If c is lowercase or uppercase, the value is calculated as1 if (lowercase || uppercase) printf("%c is a vowel", c); else printf("%c is a consonant", c); } return 0; }
Now, if the user enters a non-alphabetic character, you will see:
Error! Non-alphabetic character.