I'm stumped on why I'm getting 25 when I type in my name for the program. It's supposed to count the vowels and last time I checked chase only has 2. Here's a picture of assignment as well.
/* Program : Ch5Ex12a - CountVowels
* Programmer : Chase Mitchell
* Date : 11/18/2015
* Description : User's vowels are counted
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ch5Ex12a
{
class Program
{
static void Main(string[] args)
{
int bacon=0;
string Phrase;
Console.WriteLine("Enter in letters");
Phrase = Console.ReadLine();
foreach (char a in Phrase)
bacon += 1;
foreach (char e in Phrase)
bacon += 1;
foreach (char i in Phrase)
bacon += 1;
foreach (char o in Phrase)
bacon += 1;
foreach (char u in Phrase)
bacon += 1;
Console.WriteLine(bacon);
Console.ReadKey();
}
}
}
foreach (char a in Phrase)
bacon += 1;
What do you think this does? This does not iterate over all 'a'
characters in Phrase
. Instead, it iterates over all characters in Phase
and counts them. It’s just that the variable name that gets assigned each character in each iteration is called a
. But that has nothing to do with the content.
You tried to do something like this:
foreach (char c in Phrase)
{
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
bacon += 1;
}
You should also check for upper-case characters; either explicitly or by converting the Phrase
into lower-case first. You could do that by looping over Phrase.ToLower()
instead of Phrase
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With