C# Count Vowels

后端 未结 17 1755
小鲜肉
小鲜肉 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条回答
  •  旧时难觅i
    2020-12-03 09:36

    We can use regular expression to match vowels in a sentence.

    Regex.Matches() function will return an array with all occurrence of vowel. Then we can use the count property to find the count of vowels.

    Regular expression to match vowels in a string: [aeiouAEIOU]+

    Below is the working code snippet:

     public static void Main()
       {
           string pattern = @"[aeiouAEIOU]+";
           Regex rgx = new Regex(pattern);
           string sentence = "Who writes these notes?";
           Console.WriteLine(rgx.Matches(sentence).Count);
       }
    

提交回复
热议问题