字母表中的26个字母中,有5个是元音(a, e, i, o, u),其余是辅音。
要检查给定字符是元音还是辅音,我们必须将输入的字符与元音字母(a, e, i, o, u)进行比较。
要求用户输入并将其存储到字符类型变量 **c** 中。输入的字符可能是
因此,我们必须检查以上三种情况。为此,声明两个布尔类型的变量 **'lowercase'** 和 **'uppercase'**。布尔是一种数据类型,用 **关键词 `bool`** 声明,只能取 true 或 false 的值。True = 1,false = 0。
步骤 1: 调用头文件 `iostream`。
步骤 2: 使用 namespace std.
步骤 3: 打开主函数 `int main()`。
步骤 4: 声明一个字符类型变量 **c**,布尔类型变量 **lowercase**,**uppercase**。
步骤 5: 打印消息 “Enter a character”。
步骤 6: 将用户输入读取到变量 **c** 中。
步骤 7: 检查用户输入是否是字母。如果为假,则打印非字母字符。
步骤 8: 检查用户输入是否是大写元音。如果为真;打印 c 是元音。
步骤 9: 检查字符是否是小写元音。如果为真;打印 c 是元音。
步骤 10: 如果步骤 7 为真,而步骤 8 和步骤 9 为假;则打印 c 是辅音。
步骤 11: 退出。
#include <iostream>
using namespace std;
int main() {
char c;
bool lowercase, uppercase;
cout << "Enter an alphabet: ";
cin >> c;
// evaluates to 1 (true) if c is a lowercase vowel
lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 (true) if c is an uppercase vowel
uppercase= (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// show error message if c is not an alphabet
if (!isalpha(c))
printf("Error! Non-alphabetic character.");
else if (lowercase || uppercase)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";
return 0;
}
Run 1 Enter an alphabet: E E is a vowel. Run 2 Enter an alphabet: a a is a vowel. Run 3 Enter an alphabet: h h is a consonant. Run 4 Enter an alphabet: T T is a consonant. Run 5 Enter an alphabet: 5 Error! Non-alphabetic character.