-2

I am working on a program that is designed to count the vowels in a word, however I am having trouble counting the vowels per word. My current code looks like this:

    string word;
    string[] ca = { "a", "e", "i", "o", "u", "A", "E", "I", "O", "U" };
    int va = 0;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        if (ca.Contains(word))
        {

            label1.Text = "Vowel Count: " + va;
        }
        else
        {
            label1.Text = "Vowel Count: " + va;
        }
    }

Thanks for the help!

durron597
  • 31,968
  • 17
  • 99
  • 158
Mrsoldier3201
  • 23
  • 1
  • 6

2 Answers2

0

The easiest way is to split the string into words to start with. This can be achieved with the help of the string Split() method:

// you need to decide what the word separators are:
var words = text.Split(new char[]{'.', ',', ':', ';', '\r', '\t', '\n'});

Once done it's just a for loop:

foreach (var word in words)
{
    foreach (var character in word)
    {
        if (vowels.Any(x => x == character))
          ++count;
    }
}    
jgauffin
  • 99,844
  • 45
  • 235
  • 372
0

You could do it like:

string word = "myword";
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
int vowelCount = word.Count(x => vowels.Contains(Char.ToLower(x)));
Jcl
  • 27,696
  • 5
  • 61
  • 92