using System; public static class CharacterCounter { public static void Main(string[] args) { string joinedString = string.Join(" ", args); Console.WriteLine(CountCharacters('n', joinedString)); Console.WriteLine(joinedString.CountCharacters('n')); } /// /// Extension method. Counts the number of times char c shows up in the current instance of a String /// /// /// string s = "jerry"; /// Console.WriteLine(s.CountCharacters('r')); /// public static int CountCharacters(this string str, char c) { return CountCharacters(c, str); } /// /// Counts the number of times char c shows up in String str /// public static int CountCharacters(char c, string str) { int currentIndex = 0; int count = 0; while (str.IndexOf(c, currentIndex) > -1) { currentIndex = str.IndexOf(c, currentIndex) + 1; count++; } return count; } }