C# Count Vowels

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

    This is how I would handle this.

    var sentence = "Hello my good friend";
                var sb = sentence.ToLower().ToCharArray();
                var count = 0;
                foreach (var character in sb)
                {
                    if (character.Equals('a') || character.Equals('e') || character.Equals('i') || character.Equals('o') ||
                        character.Equals('u'))
                    {
                        count++;
                    }
                }
    

提交回复
热议问题