Check if a character is a vowel or consonant?

前端 未结 13 1267
别那么骄傲
别那么骄傲 2020-12-09 09:57

Is there a code to check if a character is a vowel or consonant? Some thing like char = IsVowel? Or need to hard code?

case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’         


        
13条回答
  •  佛祖请我去吃肉
    2020-12-09 10:36

    Try this out:

    char[] inputChars = Console.ReadLine().ToCharArray();
    int vowels = 0;
    int consonants = 0;
    foreach (char c in inputChars)
    {
       if ("aeiou".Contains(c) || "AEIOU".Contains(c))
       {
           vowels++;
       }
       else
       {
           consonants++;
       }
    }
    Console.WriteLine("Vowel count: {0} - Consonant count: {1}", vowels, consonants);
    Console.ReadKey();
    

提交回复
热议问题