如何用一个例子编写C语言程序来检查元音或辅音?在英语中,A、E、I、O、U这五个字母称为元音。除了这五个字母之外的所有字母都称为辅音。
检查元音或辅音的程序
此程序允许用户输入任何字符,并使用If Else语句检查用户指定的字符是元音还是辅音。
#include <stdio.h>
int main()
{
char ch;
printf("Please Enter an alphabet: \n");
scanf(" %c", &ch);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("\n %c is a VOWEL.", ch);
}
else {
printf("\n %c is a CONSONANT.", ch);
}
return 0;
}

以下C编程语句将要求用户输入任何字符,然后我们将该字符赋给先前声明的变量ch。
printf("Please Enter an alphabet: \n");
scanf(" %c", &ch);
接下来,我们使用If Else语句来检查用户输入的字符是否等于a、e、i、o、u、A、E、I、I、O。如果为真,则为元音,否则为辅音。
请记住,您可以使用strlwr或strupr将所有字符转换为一种大小写,然后在If条件中检查小写或大写元素。
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("\n %c is a VOWEL.", ch);
}
C语言检查字符是元音还是辅音——方法2
此检查元音或辅音的程序与上面的程序相同,但我们以更易读的方式安排了代码。
#include <stdio.h>
int main()
{
char ch;
int lowercase_Vowel, uppercase_Vowel;
printf("Please Enter an alphabet: \n");
scanf(" %c", &ch);
lowercase_Vowel = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
uppercase_Vowel = (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
if (lowercase_Vowel || uppercase_Vowel) {
printf("\n %c is a VOWEL.", ch);
}
else {
printf("\n %c is a CONSONANT.", ch);
}
return 0;
}
Please Enter an alphabet:
I
I is a VOWEL.
输出2:让我们输入一个辅音值
Please Enter an alphabet:
g
g is a CONSONANT.
使用函数查找元音或辅音的程序
此程序允许用户输入任何字符,并使用函数查找用户指定的字符是元音还是辅音。
#include <stdio.h>
int check_vowel(char a);
int main()
{
char ch;
printf("Please Enter an alphabet: \n");
scanf(" %c", &ch);
if(check_vowel(ch)) {
printf("\n %c is a VOWEL.", ch);
}
else {
printf("\n %c is a CONSONANT.", ch);
}
return 0;
}
int check_vowel(char c)
{
if (c >= 'A' && c <= 'Z')
c = c + 32;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
return 1;
return 0;
}
Please Enter an alphabet:
e
e is a VOWEL.
输出2:让我们输入一个辅音值
Please Enter an alphabet:
M
M is a CONSONANT.
C语言使用ASCII值检查元音或辅音的程序
此C程序允许用户输入任何字符。接下来,它将使用ASCII值检查指定的字符是元音还是辅音。
#include <stdio.h>
int main()
{
char ch;
printf("Please Enter an alphabet: \n");
scanf(" %c", &ch);
if(ch == 97 || ch == 101 || ch == 105 || ch == 111 || ch == 117 ||
ch == 65 || ch == 69 || ch == 73 || ch == 79 || ch == 85)
{
printf("%c is a VOWEL.\n", ch);
}
else if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
{
printf("%c is a CONSONANT.\n", ch);
}
return 0;
}
Please Enter an alphabet:
u
u is a VOWEL.
让我们输入一个辅音值
Please Enter an alphabet:
K
K is a CONSONANT.