C# Count Vowels

后端 未结 17 1761
小鲜肉
小鲜肉 2020-12-03 08:59

I am learning to program C# and I am trying to count the vowels. I am getting the program to loop through the sentence, but instead of returning vowel count, it is just retu

17条回答
  •  囚心锁ツ
    2020-12-03 09:22

    // Using two loops.

            char[] vowels= new char[]{'a', 'e', 'i', 'o', 'u',
                                       'A', 'E', 'I', 'O', 'U'}; 
            string myWord= "This is a beautiful word.";
            
            int numVowels = 0;
            
            foreach(char c in  myWord.ToCharArray())
            {
                foreach(char c2 in vowels)
                {
                    if(c == c2) numVowels++;    
                }  
            }
            
            Console.WriteLine($"{numVowels} vowels in: {myWord}");
    

提交回复
热议问题