需要在字符串中显示每个单词的平均字母并显示

发布于 2025-01-23 14:06:46 字数 334 浏览 0 评论 0原文

这是我到目前为止所拥有的,它设置为显示我需要保留或修改以保留的单词总数,以允许显示每个单词中的平均字母数量,如果可以的话,请帮助任何人。非常

{
     string words = tbxArgument.Text.Trim();
     MessageBox.Show("Number of Words: " + CountWords(words));
}

感谢

{
     string[] allWords = words.Split(' ');
     return allWords.Length;
}

Here is what I have so far, it is set up to display the total number of words, which I need to keep, or modify to keep to allow the average number of letters in each word to be displayed, please assist anyone if you can. thank you so much!:

private void btnCheck_Click(object sender, EvenArgs e)

{
     string words = tbxArgument.Text.Trim();
     MessageBox.Show("Number of Words: " + CountWords(words));
}

private int CountWords(string words)

{
     string[] allWords = words.Split(' ');
     return allWords.Length;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挥剑断情 2025-01-30 14:06:46

不确定这是否是您要完成的工作。

但这将为您提供所有单词的平均长度。

double totalCharacters = 0;
double avgCharacters = 0;
string[] words = new string[] {"Word1","Word2","Word3" } ;


foreach (string tmpString in words)
{
    totalCharacters = totalCharacters + tmpString.Length;
}

avgCharacters = totalCharacters/words.Length;

Not sure If this is what you are trying to accomplish.

But this will give you the average length of all the words.

double totalCharacters = 0;
double avgCharacters = 0;
string[] words = new string[] {"Word1","Word2","Word3" } ;


foreach (string tmpString in words)
{
    totalCharacters = totalCharacters + tmpString.Length;
}

avgCharacters = totalCharacters/words.Length;
轻许诺言 2025-01-30 14:06:46

这是一种仅在字符串上结合的方法,而无需分配需要额外的内存。只是一种优化,用于娱乐:

public static double GetAvgLetters(string text, out int wordsCount)
{
    wordsCount = 0;
        
    if (string.IsNullOrWhiteSpace(text))
    {
        return double.NaN;
    }

    var lettersCount = 0;

    var isLetter = text[0] != ' ';
    var readingLetter = isLetter;
    for (int i = 0; i < text.Length; i++)
    {
        isLetter = text[i] != ' ';

        if (isLetter != readingLetter)
        {
            readingLetter = isLetter;

            if (readingLetter)
            {
                lettersCount++;
            }
            else
            {
                wordsCount++;
            }
        }
        else if (isLetter)
        {
            lettersCount++;
        }
    }

    if (isLetter == readingLetter && isLetter)
    {
        wordsCount++;
    }

    return lettersCount / (double)wordsCount;
}

我简单地迭代检查空白和空白之间的更改(数量算词),然后在阅读字母时,计数字母。最后,如果我们正在阅读字母,而《最后的字符》是一封信,则必须添加最后一句话才能计数。

This is a method that only interate over the string, without making splits that require allocate extra memory. Just an optimization, for entertainment:

public static double GetAvgLetters(string text, out int wordsCount)
{
    wordsCount = 0;
        
    if (string.IsNullOrWhiteSpace(text))
    {
        return double.NaN;
    }

    var lettersCount = 0;

    var isLetter = text[0] != ' ';
    var readingLetter = isLetter;
    for (int i = 0; i < text.Length; i++)
    {
        isLetter = text[i] != ' ';

        if (isLetter != readingLetter)
        {
            readingLetter = isLetter;

            if (readingLetter)
            {
                lettersCount++;
            }
            else
            {
                wordsCount++;
            }
        }
        else if (isLetter)
        {
            lettersCount++;
        }
    }

    if (isLetter == readingLetter && isLetter)
    {
        wordsCount++;
    }

    return lettersCount / (double)wordsCount;
}

I simply iterate checking the changes between blank and not blank (to count words) and, while reading letters, counting letters. At the end, if we are reading letters and last char is a letter, we must add the last word to count.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文