Skip to content

Instantly share code, notes, and snippets.

@ramjill
Created September 19, 2012 09:10
Show Gist options
  • Save ramjill/3748612 to your computer and use it in GitHub Desktop.
Save ramjill/3748612 to your computer and use it in GitHub Desktop.
Given an input string (of no more than 300 characters in length), parse the string and count the occurrences of vowels and consonants. The data should be output in the format below. Punctuation should be ignored. {vowels:<count>,consonants:<count>} eg:
public class Challenge
{
public static void Main()
{
Console.WriteLine("Enter String Name");
string inputString = Console.ReadLine();
string alphabet = new String(inputString.Where(Char.IsLetter).ToArray());
int vowel = CountVowels(alphabet);
int consonants = CountConsonants(alphabet);
Console.WriteLine("{0}vowels:{1},consonants:{2}{3}", "{", vowel, consonants, "}");
Console.Read();
}
public static int CountVowels(string value)
{
const string vowels = "aeiou";
return value.Count(chr => vowels.Contains(char.ToLowerInvariant(chr)));
}
public static int CountConsonants(string value)
{
const string vowels = "aeiou";
return value.Count(chr => !vowels.Contains(char.ToLowerInvariant(chr)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment