字符串中单个单词的计数..有人可以帮我吗? [英] Count of individual words in a string..Could anyone help me out?

查看:67
本文介绍了字符串中单个单词的计数..有人可以帮我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入应为任何字符串.例如,孔雀是鸟"
输出应显示为..

a-2,
孔雀-1,
是-1,
鸟-1


任何人都可以用代码(C#)或某些想法帮助我吗?

Input should be of any string .. For example, "A peacock is a bird"
Output Should display as..

a - 2,
peacock - 1,
is - 1,
bird - 1


Can anyone please help me out in code(C#) or some idea?

public void CountIndividualWords()
       {
           string strInput = "A peacock is a bird";
           string[] strWord = strInput.ToString().ToLower().Split(' ');
           for (int i = 0; i < strWord.Length; i++)
           {

           }

       }




在此先感谢




Thanks in advance,

推荐答案

在这里看我的文章: ^ ]

我有一个提取单词统计信息的函数.
Take a look at my article here : hOOt - full text search engine[^]

I have a function that extracts word statistics.


static void CountWordOccurrence()
{
    //tell the user to enter their sentence
    Console.WriteLine("\nPlease enter the sentence to be tested.");
    //split their input into a string array
    string[] words = Console.ReadLine().Split('' '');
    //create our SortedList for holding our words and their count
    SortedList wordList = new SortedList();
    //variable to hold how many times a word appears in the string
    int numWords = 0;
    //now we loop through the string array
    foreach (string word in words)
    {
        //check and see if this word is in our list yet
        if (!(wordList.ContainsKey(word)))
        {
            //it isnt there so we add it with a value of 1
            //since it''s the words first occurrence
            wordList.Add(word, 1);
            //increment our word counter
            numWords++;
        }
        else
        {
            //since the word dous exist we get the count of times it exists
            int iWordCount = (int)wordList[word];
            //we then increment the count for that word in the list
            wordList[word] = iWordCount + 1;
        }
    }
    //now we need an enumerator so we can traverse the list. For
    //this we will use the IDictionaryEnumerator and assign that the
    //value of the GetEnumerator method of our Sorted List
    IDictionaryEnumerator enumerator = wordList.GetEnumerator();
    //now we use the MoveNext method of our enumerator
    //to tell us whether we''re at the end of our list
    while (enumerator.MoveNext())
    {
        //write a blank line
        Console.WriteLine();
        //now write our the occurrence of each word in the sentence using
        //the enumerator.Key (the word) and the enumerator.Value (the count)
        Console.WriteLine(string.Format("Word {0} Appeared {1} Times", enumerator.Key, enumerator.Value));
        Console.WriteLine();
    }
    Console.ReadKey(true);
}




答案是从这里来的....
http://www.dreamincode.net/code/snippet2682.htm [




the answer is from here....
http://www.dreamincode.net/code/snippet2682.htm[^]

mark as answer if solved your problem... it motivates :)


有很多方法可以做到这一点.我在大约5分钟的时间内完成了此操作,这与发布的解决方案是一种类似但不同的方法.

There are many many ways to do this. I whipped this up in about 5 minutes it is a similar but different approach to the solutions posted.

Dictionary<string,int> occurrences = new Dictionary<string,int>();
string strInput = "A peacock is a bird";
string[] strWord = strInput.ToString().ToLower().Split(' ');
for (int i = 0; i < strWord.Length; i++)
{
    if (!occurrences.ContainsKey(strWord[i]))
    {
        int n = 0;
        int result = strWord.Count<string>(
            delegate { return strWord[i] == strWord[n++]; });
        occurrences.Add(strWord[i], result);
    }
}



还有我用来查看结果的代码



and the code I used to view the results

for (int i = 0; i < occurrences.Count; i++)
{
    MessageBox.Show(occurrences.Keys.ElementAt(i) + " - " + 
        occurrences.Values.ElementAt(i));
}


这篇关于字符串中单个单词的计数..有人可以帮我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆