Check if a character is a vowel or consonant?

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

    This works just fine.

    public static void Main(string[] args)
        {
            int vowelsInString = 0;
            int consonants = 0;
            int lengthOfString;
            char[] vowels = new char[5] { 'a', 'e', 'i', 'o', 'u' };
    
            string ourString;
            Console.WriteLine("Enter a sentence or a word");
            ourString = Console.ReadLine();
            ourString = ourString.ToLower();
    
            foreach (char character in ourString)
            {
                for (int i = 0; i < vowels.Length; i++)
                {
                    if (vowels[i] == character) 
                    {
                        vowelsInString++;
                    }
                }
            }
            lengthOfString = ourString.Count(c => !char.IsWhiteSpace(c)); //gets the length of the string without any whitespaces
            consonants = lengthOfString - vowelsInString; //Well, you get the idea.
            Console.WriteLine();
            Console.WriteLine("Vowels in our string: " + vowelsInString);
            Console.WriteLine("Consonants in our string " + consonants);
            Console.ReadKey();
        }
    }
    

提交回复
热议问题