C# Count Vowels

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

    There are many ways to skin a cat :-) In programming a little lateral thinking can be useful...

    total += sentence.Length - sentence.Replace("a", "").Length;
    total += sentence.Length - sentence.Replace("e", "").Length;
    total += sentence.Length - sentence.Replace("i", "").Length;
    total += sentence.Length - sentence.Replace("o", "").Length;
    total += sentence.Length - sentence.Replace("u", "").Length;
    

    You could, for example, try removing a vowel from the sentence and looking if the sentence is smaller without the vowel, and by how much.

提交回复
热议问题