-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount_consonants.cs
37 lines (27 loc) · 1.03 KB
/
Count_consonants.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Collections.Generic;
using System.Linq;
namespace CodingChallenges;
[TestClass]
public class Count_consonants {
/*
Count consonants
Complete the function that takes a string of English-language text and returns the number of consonants in the string.
Consonants are all letters used to write English excluding the vowels a, e, i, o, u.
*/
[DataTestMethod]
[DataRow("", 0)]
[DataRow("aaaaa", 0)]
[DataRow("Bbbbb", 5)]
[DataRow("helLo world", 7)]
[DataRow("h^$&^#$&^elLo world", 7)]
[DataRow("012456789", 0)]
[DataRow("012345_Cb", 2)]
public void Test(string str, int expected) => Assert.AreEqual(expected, ConsonantCount(str));
public static int ConsonantCount(string str) => str.WhereOnlyConsonants().Count();
}
public static class MyExtensions {
public static IEnumerable<char> WhereOnlyConsonants(this IEnumerable<char> chars) {
foreach (char c in chars)
if (char.IsLetter(c) && !"aeiou".Contains(char.ToLower(c))) yield return c;
}
}