Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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# - Fatal编程技术网

C# 计算字符串c中的元音#

C# 计算字符串c中的元音#,c#,C#,我正在制作一个程序来分析用户输入句子的特征。为了计算句子中的元音,我在这里做了一系列非常混乱的foreach循环: foreach (char v1 in input) { if (v1 == 'a') { vcounter++; } } foreach (char v2 in input) { if (v2 == 'e') { vcounter++; } } foreach (char v3 in input) {

我正在制作一个程序来分析用户输入句子的特征。为了计算句子中的元音,我在这里做了一系列非常混乱的foreach循环:

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

然后我获取vcounter并将其显示为结果。我是c#的新手,我想知道是否有人可以建议一种更好的方法来做这件事?

如果您使用的是.NET 3.5或更高版本,您可以使用LINQ来做这件事

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');
为此,必须在文档中导入LINQ

using System.Linq;

您可以尝试使用以下代码:

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

for(int i=0;i
将元音存储在数组中,如下所示:

private readonly char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
然后数一数:

public int CountVowels(string text)
{
    return text.ToLower().Count(x => vowels.Contains(x));
}

将元音存储在列表中:

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

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());
将创建一个类似于马克西姆答案的字典,但只包含元音


使用
元音频率.Sum(vf=>vf.Value)
获取字符串中元音的总计数。

效率不高,但很短

using System.Text.RegularExpressions;

// ...

var vcounter = Regex.Matches(input, "[aeiou]", RegexOptions.IgnoreCase).Count;

您可以只使用正则表达式:

vcounter = Regex.Matches(input, "a|e|i|o|u", RegexOptions.IgnoreCase).Count;

您可以在该函数中进行维护。我创建了一个包含一些字母的数组

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

提示:考虑一个字典,如果你创建一个哈希集而不是一个数组,它会更快:)包含在数组中是真正的全扫描。@ IlyaChernomordik的小集全扫描可能实际上更快。无论如何,这是一个微观优化。;-)你不需要逃避输入。只有当您试图将普通字符串转换为匹配表达式时,才需要这样做。这一点很好!我已经更新了答案。
vcounter = Regex.Matches(input, "a|e|i|o|u", RegexOptions.IgnoreCase).Count;
 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());
        }