Wednesday, 7 August 2013

C# Count Vowels

C# Count Vowels

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 returning the length of the string. Any help would
be greatly appreciated.
static void Main()
{
int total = 0;
Console.WriteLine("Enter a Sentence");
string sentence = Console.ReadLine().ToLower();
for (int i = 0; i < sentence.Length; i++)
{
if (sentence.Contains("a") || sentence.Contains("e") ||
sentence.Contains("i") || sentence.Contains("o") ||
sentence.Contains("u"))
{
total++;
}
}
Console.WriteLine("Your total number of vowels is: {0}", total);
Console.ReadLine();
}

No comments:

Post a Comment