-2

I am making a program to analyse features of a user inputted sentence. To count the vowels in the sentence I have made a very messy sequence of foreach loops here:

foreach (char v1 in input)
{
    if (v1 == 'a')
    {
        vcounter++;
    }
}
foreach (char v2 in input)
{
    if (v2 == 'e')
    {
        vcounter++;
    }
}
foreach (char v3 in input)
{
    if (v3 == 'i')
    {
        vcounter++;
    }
}
foreach (char v4 in input)
{
    if (v4 == 'o')
    {
        vcounter++;
    }
}
foreach (char v5 in input)
{
    if (v5 == 'u')
    {
        vcounter++;
    }
}

I then take the vcounter and display it as the result. I am very new to c# and I am wondering if anyone can suggest a better way of doing this?.

rene
  • 41,474
  • 78
  • 114
  • 152
apaaaan
  • 31
  • 1
  • 10

8 Answers8

8

Store your vowels in array like this:

private readonly char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };

And then count them:

public int CountVowels(string text)
{
    return text.ToLower().Count(x => vowels.Contains(x));
}
Maciej Kasprzak
  • 929
  • 6
  • 17
0

If your are using .NET 3.5 or higher you could use LINQ for this.

vcounter += input.Count(v => v == 'a');
vcounter += input.Count(v => v == 'e');
vcounter += input.Count(v => v == 'i');
vcounter += input.Count(v => v == 'o');
vcounter += input.Count(v => v == 'u');

For this you must import LINQ in your document.

using System.Linq;
Nik Bo
  • 1,410
  • 2
  • 17
  • 29
0

You can try with following code:

const string vowels = "aeiou";
var count = input.Count(chr => vowels.Contains(char.ToLower(chr)));

or

for (int i = 0; i < input.Length; i++)
    {
        if (input[i]  == 'a' || input[i] == 'e' || input[i] == 'i' || input[i] == 'o' || input[i] == 'u')
        {
            count++;
        }
    }
Jaimin Dave
  • 1,224
  • 10
  • 18
0

Store your vowels in list :

List<Char> vowels = new List<char>() { 'a', 'e', 'i', 'o', 'u' };

And count like this :

int vcounter = 0;
foreach (char chr in input)
{
   if (vowels.Contains(chr))
   {
       vcounter++;
   }
}
Sameer
  • 2,143
  • 1
  • 16
  • 22
0

Try this:

var text = "hfdaa732ibgedsoju";
var vowels = new[] { 'a', 'e', 'i', 'o', 'u' };
var vowelFrequency = text.Where(c => vowels.Contains(c))
                         .GroupBy(c => c)
                         .ToDictionary(g => g.Key, g.Value.Count());

Will create a dictionary similar to Maksim's answer but only with vowels.

Use vowelFrequency.Sum(vf => vf.Value) to get the total count of vowels in the string.

silkfire
  • 24,585
  • 15
  • 82
  • 105
0

Not very efficient, but short

using System.Text.RegularExpressions;

// ...

var vcounter = Regex.Matches(input, "[aeiou]", RegexOptions.IgnoreCase).Count;
Hafthor
  • 16,358
  • 9
  • 56
  • 65
0

You can just use a regular expression:

vcounter = Regex.Matches(input, "a|e|i|o|u", RegexOptions.IgnoreCase).Count;
thomasb
  • 5,816
  • 10
  • 57
  • 92
Andy Lamb
  • 2,151
  • 20
  • 22
0

You can maintain in this function. I created a array which contains some letters.

 private void button1_Click(object sender, EventArgs e)
        {
            string [] array = { "a", "b", "c", "d" };
            int vcounter = 0;
            foreach (var item in array)
            {
                if (item.Contains("a") || item.Contains("e") || item.Contains("i")) //You can add the other volwes 
                {
                    vcounter++;
                }
            }
            MessageBox.Show("Count of vowels : " + vcounter.ToString());
        }
Bertug
  • 915
  • 2
  • 10
  • 26