C 语言实例 - 判断元音/辅音
判断输入的字母是元音,还是辅音。
英语有26个字母,元音只包括 a、e、i、o、u 这五个字母,其余的都为辅音。y是半元音、半辅音字母,但在英语中都把他当作辅音。
实例
#include <stdio.h>
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;
printf("输入一个字母: ");
scanf("%c",&c);
// 小写字母元音
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// 大写字母元音
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// if 语句判断
if (isLowercaseVowel || isUppercaseVowel)
printf("%c 是元音", c);
else
printf("%c 是辅音", c);
return 0;
}
运行结果:
输入一个字母: G G 是辅音
C 语言实例



叶子飘飘真人
137***020@qq.com
如果从键盘中输入的不是字母,例如,数字,标点符号,都会输出是辅音。所以我对这个做了一个简单的判定。
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <ctype.h> int main() { char cLetter; int iInRange, iCheck, i, iLetter; i = 1; while (i) { printf("输入一个字母, 判断元音与辅音. 如果想退出,请输入“0”。\n"); scanf("%c", &cLetter); getchar(); printf("----------------------------------------------------------------------\n"); iLetter = (int)cLetter; iCheck = isalpha(iLetter); if (iCheck) { iInRange = (cLetter == 'a') || (cLetter == 'e') || (cLetter == 'i') || (cLetter == 'o') || (cLetter == 'u') || (cLetter == 'A') || (cLetter == 'E') || (cLetter == 'I') || (cLetter == 'O') || (cLetter == 'U'); if (iInRange) { printf("字母 %c 是元音字母!\n", cLetter); printf("----------------------------------------------------------------------\n"); continue; } else { printf("字母 %c 是辅音字母!\n", cLetter); printf("----------------------------------------------------------------------\n"); continue; } } else if (iCheck == 0) { if (iLetter != 48) { printf("Error input!\n"); printf("----------------------------------------------------------------------\n"); continue; } else if ((int)cLetter == 48) { printf("Bye bye~~~!\n"); printf("----------------------------------------------------------------------\n"); i = (int)cLetter - 48; } } } return 0; }叶子飘飘真人
137***020@qq.com