C#程序用于计算已发送的单词 [英] C# program for count words in sentance

查看:100
本文介绍了C#程序用于计算已发送的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写ac#程序,其输入为 Twinkle,Twinkle,Little Star

它将输出为

Twinkle - 2,

Little - 1,

Star - 1


表示带有计数的单词一句话。



谢谢。

How to write a c# program in which it takes input as "Twinkle, Twinkle, Little Star" .
It will give output as
Twinkle - 2,
Little - 1,
Star - 1

means word with its count in a sentence .

Thanks.

推荐答案

你可以试试这个:

You can try this:
string sentence = "Twinkle, Twinkle, Little Star";
string[] words = sentence.Split(new char[] { ' ', ',', '.', '!', ':', '?', ';' }, StringSplitOptions.RemoveEmptyEntries); // this splits the string into the words
var groups = words.GroupBy(x => x); // this groups the words
foreach (var group in groups)
{
    Console.WriteLine("{0} - {1}", group.Key, group.Count());
}



如何工作:将句子分成单词后,将单词分组。然后迭代所有组,然后打印键(单词)和组中单词的计数。


How this works: after splitting the sentence into words, you group the words. Then you iterate over all groups and you print the key (the word) and the count of words in the group.


请参阅C# String Occurrence Count [ ^ ]


你好,



试试这个:





Hello,

try out this :


static void Main(string[] args)
       {

           //string sentence = "twinkle, twinkle, little star";
           do
           {
               Console.WriteLine("Enter a sentence");
               string sentence = Console.ReadLine();

               string[] data = sentence.Split(',', ' ');
               int count = 0;
               int original_count = data.Length;
               for (int i = 0; i < data.Length; i++)
               {
                   if (data[i] == "")
                       count++;
                   Console.WriteLine(data[i]);
               }

               original_count = original_count - count;
               Console.WriteLine(original_count.ToString());
               Console.WriteLine("Press enter to continue.......");
               Console.ReadLine();
               Console.Clear();

           } while (true);

       }







- 这是一个控制台应用程序,当你运行时在提示时,只需输入您想要查找其计数的句子。你还需要添加尽可能多的分隔符组合,因为你可以认为一个人可以放入他的句子。

- 我已经添加了一些。

- 尝试不同的组合,请告诉我。



谢谢,

Rahul




- This is a console application, when you run it when prompted simply enter the sentence whose count you want to find out. Also you need to add as many possible combinations of delimiters as you can think a person can put in his sentence.
- I have added a few.
- Try out different combinations and please do tell me.

Thanks,
Rahul


这篇关于C#程序用于计算已发送的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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