Reputation: 545
I am teaching myself C# and one of the current chapters challenges asks me to prompt the user for a string, write back the string, count the number of characters, the instances of the letter 'e' and finally the instances of all vowels. It gave a hint to use switch
but I couldn't figure out how to do it. I did get it to work by doing it manually, but I don't think that's the point. :) How could I use a switch
statement to reduce the number of typed lines?
Console.WriteLine("Please type a sentence and hit enter: ");
string myString = Console.ReadLine();
int letterCount = myString.Split('e').Length - 1;
Console.Clear();
Console.WriteLine("Thank you. The sentence you entered was: \n\"{0}\"", myString);
Console.WriteLine("This sentence is {0} characters long.", myString.Length);
Console.WriteLine("It contains {0} instances of the letter \'e\'.", letterCount);
int vowelCount = 0;
int letterALower = myString.Split('a').Length - 1;
vowelCount += letterALower;
int letterELower = myString.Split('e').Length - 1;
vowelCount += letterELower;
int letterILower = myString.Split('i').Length - 1;
vowelCount += letterILower;
int letterOLower = myString.Split('o').Length - 1;
vowelCount += letterOLower;
int letterULower = myString.Split('u').Length - 1;
vowelCount += letterULower;
int letterAUpper = myString.Split('A').Length - 1;
vowelCount += letterAUpper;
int letterEUpper = myString.Split('E').Length - 1;
vowelCount += letterEUpper;
int letterIUpper = myString.Split('I').Length - 1;
vowelCount += letterIUpper;
int letterOUpper = myString.Split('O').Length - 1;
vowelCount += letterOUpper;
int letterUUpper = myString.Split('U').Length - 1;
vowelCount += letterUUpper;
Console.WriteLine("There are {0} vowels used.", vowelCount);
Console.ReadLine();
Upvotes: 0
Views: 879
Reputation: 6814
Here's a simple solution:
string str = Console.ReadLine();
string low_str = str.ToLower();
Console.Clear();
Console.WriteLine("Thank you. The sentence you entered was: \n\"{0}\"", str);
Console.WriteLine("This sentence is {0} characters long.", str.Length);
int vowelCount = 0;
int eCount = 0;
for (int i = 0; i < low_str.Length; i++)
{
switch(low_str[i])
{
case 'e': eCount ++; vowelCount++; break;
case 'a': vowelCount++; break;
case 'o': vowelCount++; break;
case 'i': vowelCount++; break;
case 'u': vowelCount++; break;
case 'y': vowelCount++; break;
}
}
Console.WriteLine("It contains {0} instances of the letter \'e\'.", eCount);
Console.WriteLine("There are {0} vowels used.", vowelCount);
Console.ReadLine();
Notice that this could be done in an even fewer lines using this method (not the best way, but let's not go too deep into the framework details :) ):
int eCount = low_str.split(new char[]{'e'}) - 1;
int vowelCount = low_str.split(new char[]{'a','e','o','i','u','y'}) - 1;
Upvotes: 2
Reputation: 120380
I know this isn't a good answer to the question, but I couldn't resist a one liner!
inputString.ToLower().Count(s=>"aeiou".Contains(s)); //count the vowels
Upvotes: 4
Reputation: 828
I personally would do it with foreach and an array or vowels. This way it's easy to expand, like this:
Char[] vowels = {'e', 'a', 'o', 'i', 'u', 'y'};
string str = Console.ReadLine();
string low_str = str.ToLower();
Console.Clear();
Console.WriteLine("Thank you. The sentence you entered was: \n\"{0}\"", str);
Console.WriteLine("This sentence is {0} characters long.", str.Length);
int vowelCount = 0;
int eCount = 0;
foreach (char chara in low_str)
{
foreach (char vowel in vowels)
if (vowel == chara)
vowelCount++;
if (chara == 'e')
eCount++;
}
Console.WriteLine("It contains {0} instances of the letter \'e\'.", eCount);
Console.WriteLine("There are {0} vowels used.", vowelCount);
Console.ReadLine();
Upvotes: 1
Reputation: 121599
Something like this (pseudo code, not actual C#)?
foreach (c in mySentence)
{
c = LowerCase(c);
switch (c) {
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
nVowels ++;
break;
case ' ' :
case '\t' :
nBlanks++;
break;
default :
nChars++
break;
}
Here's a bit more info:
Upvotes: 1