Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何计算字符串C中的元音#_C#_Windows - Fatal编程技术网

C# 如何计算字符串C中的元音#

C# 如何计算字符串C中的元音#,c#,windows,C#,Windows,我正在开发一个程序,该程序旨在计算一个单词中的元音,但是我在计算每个单词的元音时遇到了困难。我当前的代码如下所示: string word; string[] ca = { "a", "e", "i", "o", "u", "A", "E", "I", "O", "U" }; int va = 0; public Form1() { InitializeComponent(); } private void button1

我正在开发一个程序,该程序旨在计算一个单词中的元音,但是我在计算每个单词的元音时遇到了困难。我当前的代码如下所示:

    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;
        }
    }

谢谢你的帮助

最简单的方法是将字符串拆分为单词开始。这可以通过字符串
Split()
方法实现:

// you need to decide what the word separators are:
var words = text.Split(new char[]{'.', ',', ':', ';', '\r', '\t', '\n'});
一旦完成,它只是一个for循环:

foreach (var word in words)
{
    foreach (var character in word)
    {
        if (vowels.Any(x => x == character))
          ++count;
    }
}    
你可以这样做:

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

您现在所写的检查单词是否存在于元音数组中,当然它不存在。