C# Count Vowels

后端 未结 17 1772
小鲜肉
小鲜肉 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:33

    You can also do this with switch statement

            var total = 0;
            var sentence = "Hello, I'm Chris";
            foreach (char c in sentence.ToLower())
            {
                switch (c)
                {
                    case 'a':
                    case 'e':
                    case 'i':
                    case 'o':
                    case 'u':
                        total++;
                        break;
                    default: continue;
                }
    
            }
            Console.WriteLine(total.ToString());
    

提交回复
热议问题