元音是字母a、e、i、o、u。所有其余的字母都称为辅音。
检查字符是元音还是辅音的程序如下 -
#include <iostream> using namespace std; int main() { char c = 'a'; if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ) cout <<c<< " is a Vowel" << endl; else cout <<c<< " is a Consonant" << endl; return 0; }输出结果
a is a Vowel
在上面的程序中,使用 if 语句来查找字符是 a、e、i、o 还是 u。如果是这些中的任何一个,它就是元音。否则就是辅音。
这显示在下面的代码片段中。
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ) cout <<c<< " is a Vowel" << endl; else cout <<c<< " is a Consonant" << endl;
上面的程序只检查小写字符。因此,检查大写和小写字符的程序如下 -
#include <iostream> using namespace std; int main() { char c = 'B'; if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') cout <<c<< " is a Vowel" << endl; else cout <<c<< " is a Consonant" << endl; return 0; }
B is a Consonant
在上面的程序中,if 语句用于查找字符是否为 a、e、i、o 或 u(大写和小写)。如果是这些中的任何一个,则它是元音。否则就是辅音。
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') cout <<c<< " is a Vowel" << endl; else cout <<c<< " is a Consonant" << endl;