Well, iterate over the string and count. To avoid clutter (and errors) in your code, try not to embed all the code in one Main method.
// Model: what is/are vowel. // So if you're asked to count vowels in, say, Russian you know what to change private static HashSet<Char> s_Vowels = new HashSet<Char>() { 'a', 'e', 'i', 'o', 'u', // is 'w' considered being vowel? 'A', 'E', 'I', 'O', 'U', }; // Routine: count vowels in a given phrase // No input/output here, so it will work in WinForms, Command Line, ASP... private static int CountVowels(String phrase) { if (String.IsNullOrEmpty(phrase)) return 0; int result = 0; foreach (char letter in phrase) if (s_Vowels.Contains(letter)) result += 1; return result; } // Input/output static void Main(string[] args) { Console.WriteLine("Enter in letters"); string Phrase = Console.ReadLine(); Console.WriteLine(String.Format("It contains {0} vowels.", CountVowels(Phrase))); }
Edit : your source code has a line using System.Linq; if you post it intentionally and you are allowed to use Linq, the procedure may be shortened:
private static int CountVowels(String phrase) { if (String.IsNullOrEmpty(phrase)) return 0; else return phrase.Count(letter => s_Vowels.Contains(letter)); }
source share