Check if a character is a vowel or consonant?

前端 未结 13 1251
别那么骄傲
别那么骄傲 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:28

    Here's a function that works:

    public static class CharacterExtentions
    {
        public static bool IsVowel(this char c)
        {
            long x = (long)(char.ToUpper(c)) - 64;
            if (x*x*x*x*x - 51*x*x*x*x + 914*x*x*x - 6894*x*x + 20205*x - 14175 == 0) return true;
            else return false;
        }
    }
    

    Use it like:

    char c = 'a';
    if (c.IsVowel()) { // it's a Vowel!!! }
    

    (Yes, it really works, but obviously, this is a joke answer. Don't downvote me. or whatever.)

提交回复
热议问题