首页 > 试题广场 >

判断是元音还是辅音

[编程题]判断是元音还是辅音
  • 热度指数:52304 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
KiKi开始学习英文字母,BoBo老师告诉他,有五个字母A(a), E(e), I(i), O(o),U(u)称为元音,其他所有字母称为辅音,请帮他编写程序判断输入的字母是元音(Vowel)还是辅音(Consonant)。

输入描述:
多组输入,每行输入一个字母。


输出描述:
针对每组输入,输出为一行,如果输入字母是元音(包括大小写),输出“Vowel”,如果输入字母是非元音,输出“Consonant”。
示例1

输入

A
b

输出

Vowel
Consonant
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = "AaEeIiOoUu";
        while(sc.hasNext()){
            String s = sc.next();
            if(str.contains(s))
                System.out.println("Vowel");
            else
                System.out.println("Consonant");
        }
    }
}

发表于 2021-07-19 16:18:24 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        char ch;
        while(in.hasNext()) //表示是否将要有输入
        {
            /*输入字符串再取第一个字符
            (用字符串是因为没有nextChar()函数)*/
            ch=in.next().charAt(0);
            //将字符统一转换为大写的,这样可以少写些判断条件
            ch=Character.toUpperCase(ch);
            //判断语句
            if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
            {
                System.out.println("Vowel");//是元音时输出
            }
            else{
                System.out.println("Consonant");//是辅音时输出
            }
        }
    }
}

发表于 2022-07-11 19:17:41 回复(0)
#include <iostream>
#include <set>

using namespace std;

int main()
{
    set<char> hash = {'A','a','E','e','I', 'i', 'O', 'o', 'U','u'};
    char c;
    while(cin >> c)
    {
        if (hash.count(c)) cout << "Vowel" << endl;
        else cout << "Consonant" << endl;
    }
    
}

发表于 2022-02-26 14:06:55 回复(0)
#include<stdio.h>
int main(){
    char str;
    while(scanf("%c",&str)!=EOF){
		getchar();
        if(str=='a'||str=='e'||str=='i'||str=='o'||str=='u'||str=='A'||str=='E'||str=='I'||str=='O'||str=='U'){
            printf("Vowel\n");
        }else{
            printf("Consonant\n");
        }
    }
}

发表于 2022-01-10 22:13:49 回复(0)
#include<stdio.h>
int main()
{
	char ch = '0';

	while (scanf("%c", &ch) != EOF)
	{
		//屏蔽回车\n
		if (ch == '\n' )
			continue;

		if (ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' || ch == 'I' || ch == 'i' || ch == 'O' || ch == 'o' || ch == 'U' || ch == 'u')
			printf("Vowel\n");
		else
			printf("Consonant\n");
	}

	return 0;
}

发表于 2021-10-24 00:59:23 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        String[] num = {"A","E","I","O","U","a","e","i","o","u"};
        boolean flag = false;

        while (s.hasNext()){
            String i = s.next();
            for(int j=0;j<num.length;j++){
                if(i.equals(num[j])){
                    flag = true;
                }
            }
            if(flag){
                System.out.println("Vowel");
                flag = false;
            }else{
                System.out.println("Consonant");
            }
        }
    }
}

发表于 2021-05-06 16:06:43 回复(0)
只要有元素就匹配,不管是否出现其他元素
import java.util.Scanner;
import java.util.regex.Pattern;
public class Test46 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
while(input.hasNext())
{
	String c=input.nextLine();
	if(Pattern.compile(c,Pattern.CASE_INSENSITIVE).matcher("aeiouAEIOU").find())
		System.out.println("Vowel");
	else
		System.out.println("Consonant");

}
	}

}

发表于 2021-01-17 22:38:22 回复(0)
#include <stdio.h>
#include <ctype.h>
int main() {
    char c;
    while (scanf("%c", &c) != EOF) {
        c = tolower(c);
        if (c == '\n')
            printf("\n");
        else if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
            printf("Vowel");
        else
            printf("Consonant");
    }
     
    return 0;
}

发表于 2020-07-17 11:42:21 回复(0)
// Compiled in C++14(g++5.4)
 
#include <bits/stdc++.h>
 
using namespace std;

int main() {
    
    string vowels = "aeiouAEIOU";
    unordered_set<char> unset(vowels.begin(), vowels.end());
    char ch;
    while (cin >> ch) {
        cin.get();
        if (unset.find(ch) != unset.end()) {
            cout << "Vowel";
        } else {
            cout << "Consonant";
        }
        cout << endl;
    }
     
    return 0;
}

发表于 2020-03-24 00:33:26 回复(0)
#include<stdio.h>
int main()
{
    char ch;
    while((scanf("%c",&ch))!=EOF)
    {
        switch(ch)
        {
            case 'A':
            case 'a':
            case 'E':
            case 'e':
            case 'I':
            case 'i':
            case 'O':
            case 'o':
            case 'U':
            case 'u':printf("Vowel\n");break;
            default:printf("Consonant\n");break;
        }
        getchar();
    }
    
    return 0;
}
发表于 2021-09-09 20:49:40 回复(5)
想用C++,写了两行,算了算了。
while True:
    try:
        print("Vowel" if input() in 'aeiouAEIOU' else 'Consonant')
    except:
        break


发表于 2020-04-30 16:31:24 回复(1)
#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int ch;
    
    while ((ch = getchar()) != EOF)
    {
        if (isspace(ch))
        {
            continue;
        }
        ch = tolower(ch);
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
        {
            puts("Vowel");
        }
        else
        {
            puts("Consonant");
        }
    }
    
    return 0;
}
//库函数的运用;学C语言看C Primer Plus;
编辑于 2020-04-18 15:48:07 回复(0)
#include <stdio.h>
#include <string.h>

//解法二:

int main()
{
    char arr[] = { "AEIOUaeiou" };
    char ch = 0;
    while (~scanf(" %c", &ch))
    {
        //strchr - 在字符串中查找字符,第一个参数为数组首地址,第二个参数为要查找的字符 - 如果找得到,返回该元素地址,找不到则返回空指针
        if (strchr(arr, ch))
        {
            printf("Vowel\n");
        }
        else
        {
            printf("Consonant\n");
        }
    }
}


//解法一:

// int main()
// {
//     char arr[] = { "AEIOUaeiou" };
//     char ch = 0;
//     //多组输入
//     while (~scanf(" %c", &ch))
//     {
//         int i = 0;
//         for (i = 0; i < 10; i++)
//         {
//             if (ch == arr[i])
//             {
//                 printf("Vowel\n");
//                 break;
//             }
//         }
//         if (10 == i)
//         {
//             printf("Consonant\n");
//         }
//     }
    
//     return 0;
// }
发表于 2021-08-24 20:39:05 回复(1)
#include <stdio.h>
int main() {
    char ch;

    while (scanf("%c",&ch)!=EOF) {
        getchar();//getchar用于吸收回车,以免switch连回车键也判断了
        switch (ch) {
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':printf("Vowel\n");//十种情况定为元音
            break;
            default:printf("Consonant\n");//以上都不成立时,则为非元音
        }
    }
    return 0;
}
编辑于 2023-12-07 09:13:24 回复(0)
用 while((ch=getchar())!=EOF)后
由于换行要加\n,所以要用getchar()把n除去
发表于 2022-01-21 15:50:06 回复(0)
#include<stdio.h>
int main()
{
    char ch;
    while((ch=getchar())!=EOF){
        getchar();//getchar()是去掉换行符
        if(ch == 'a' || ch == 'A' || ch == 'E' || ch == 'e' || ch == 'I' || ch == 'i' || ch == 'O' || ch == 'o' || ch == 'U' || ch == 'u'){
      printf("Vowel\n"); 
        }     
        else{
      printf("Consonant\n");      
        }
    }
    return 0;
}

发表于 2021-11-22 15:39:09 回复(0)
#include <stdio.h>
int main(){
    char arrch[10] = {'A','a','E','e','I','i','O','o','U','u'};
    char c = 0;
    while((scanf("%c",&c)) != EOF ){
        int p = 0;
        for(int i = 0; i <= 9; i++){
            if(c == arrch[i]){
                p = 1;
                getchar();   
                break;
            }
        }
        if (p)
        {
            printf("Vowel\n");
        }else{
            printf("Consonant\n");
            getchar();
        }
    }
}
这个题可以用数组做,注意scanf带的'\n'
发表于 2021-01-25 12:51:54 回复(0)
#include <stdio.h>
int main(){
    char ch;
    while(~scanf("%c", &ch)){
    	getchar();
    	if(ch == 'a' || ch == 'A' || ch == 'E' || ch == 'e' || ch == 'I' || ch == 'i' || ch == 'O' || ch == 'o' || ch == 'U' || ch == 'u'){
    		printf("Vowel\n");} 
    	else {
    		printf("Consonant\n");
    	}   
    }
    return 0;
}

发表于 2020-03-14 21:43:03 回复(5)
#include <stdio.h>

int main()
{
    char s[] = "aeiouAEIOU";
    char* p = NULL;
    char input = 0;
    int flag = 0;

    while (scanf("%c", &input) != EOF)
    {
        //拿走空字符
        getchar();

        //重置p为字符串首元素
        p = s;
        //重置标志为0
        flag = 0;

        //判断是否为元音
        while (*p)
        {
            if (*p == input)
            {
                flag = 1;
                break;
            }
            p++;
        }

        //进行相应输出
        if (flag)
        {
            printf("Vowel\n");
        }
        else
        {
            printf("Consonant\n");
        }
    }

    return 0;
}

编辑于 2024-03-18 23:04:29 回复(0)
需要注意一下,scanf函数是会将换行符\n算作一个字符加入循环内,所以需要每次循环前输入后都要用一个getchar函数来装换行符\n
//BC53 判断是元音还是辅音
#include "stdio.h"
int main(){
    char ch;
    while((scanf("%c",&ch) != EOF)){
        getchar();
        if((ch=='A') || (ch=='E') || (ch=='I') || (ch=='O') || (ch=='U') || (ch=='a') || (ch=='e') || (ch=='i') || (ch=='o') || (ch=='u')){
            printf("Vowel\n");
            continue;
        }
        else {
            printf("Consonant\n");
        }
    }
    return 0;
}


发表于 2023-11-10 18:42:37 回复(0)

问题信息

上传者:牛客309119号
难度:
158条回答 3598浏览

热门推荐

通过挑战的用户

查看代码