如何编写一个程序,接受来自用户的字符串,对其进行标记,找到只有c#保留字的出现并给出单词的计数 [英] how to write a program that accepts string from user, tokenize it , find the occurances of only c# reserve words and give the count of the words

查看:85
本文介绍了如何编写一个程序,接受来自用户的字符串,对其进行标记,找到只有c#保留字的出现并给出单词的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助c#中的程序。什么是编写程序的最佳优化方法,该程序动态接受来自用户的字符串,对其进行标记,查找仅出现c#保留字的出现,并按排序方式给出关键字的计数。



Ex如果我的字符串是



我是编程的新手。我需要一个类示例。任何新样本都可以。我正在给新的它再次重复



程序应该发现有一些保留字用于Class and new



我的输出应该是(关键字和计数排序方式)



new - 3

class - 2.



请帮助:)



从下面的评论中复制的其他信息

我甚至试过这个并编写了如下算法



1.取一个包含c#
$ b中所有保留字列表的数组$ b 2.从用户检查字符串的用户输入以检查用户的关键字

3.拆分用户使用正则表达式的文本(检查空格或逗号或......)

4.现在我们有用户提供输入的单个标记

5.现在分组标记并且有他们的计数

5.现在我们需要迭代令牌,并且找到的每个关键字都会打印元素和计数。



I我不确定这是否是最佳方式。如果有什么需要在逻辑中纠正,请告诉我



根据上述算法,我编码如下



I need a help for a program in c#. What is the best optimized way to write a program that accepts string from user dynamically, tokenize it , find the occurances of only c# reserve words and give the count of the keywords in sorted manner.

Ex if my string is

" I am new to programming. I need a class example. Any new sample is okay. I am giving new so that it is repeated again"

Program should find that there are reserved words used like "Class and new"

My output should be ( Keyword and count in sorted way )

new - 3
class - 2.

Please help :)

additional information copied from comment below
I even tried this and wrote an algorithm as follows

1. Take an array that has the list of all reserve words in c#
2. Take a user input for string to be checked for keywords from the user
3. split the user text using regular expression ( check for space or comma or ... )
4. now we have the individual tokens of the user provided input
5. Now group the tokens and have their count
5. Now we need to iterate through tokens and for each keyword found print the element along with count.

I am not sure whether this is the optimized way to do it. Please let me know if anything needs to be corrected in the logic

Based on the above algorithm I codes as below

class CountKeyWords
{
    static void Main()
    {
        var myListOfKeywords = new [] {struct, public, class, ...};
        string userInput;

        Console.WriteLine("Enter one or more lines of text (press CTRL+Z to exit)");
        Console.WriteLine();
        do
        {
            userInput = Console.ReadLine();

        } while (userInput!= null);

        // An enumerable collection, not in memory yet.

        var keywordCount = from keyword in userInput.Split(new []{" ","(", ...}) 
                           group by keyword into g
                           where myListOfKeywords.Contains(g.Key)
                           select new {g.Key, g.Count()}

    foreach(var element in keywordCount)
    Console.WriteLine(String.Format("Keyword: {0}, Count: {1}", element.Key, element.Count));
    } // End of Main()
}// End of class CountKeyWords

Let me know if this logic is good?

推荐答案

这不是CP通常的工作方式。这里最重要的目标是学习和帮助学习。

你应该自己尝试,当你遇到困难时来到这里,对你的代码,设计等有一个具体的问题。

请查看你有什么尝试? [ ^ ]至看看我的意思是一个很好的解释。

不要忘记这里的人不会得到报酬。此外,如果我们为您提供随时可用的解决方案,它将不会帮助您,因为您不会从中学到任何东西。



< u>编辑(在算法评论之后):

我想说有几点可能对选择最佳解决方案很重要。示例:输入字符串的可能(最大)长度。



考虑到您给出的示例,我认为可以更轻松地完成。

- 使用c#

中的所有保留字列表取数组 - 使用 IndexOf link_1 [ ^ ],link_2 [ ^ ])检查当前保留字是否在字符串中。

如果找到:移动到该索引,反++并再次执行IndexOf

如果不是:检查下一个单词



换句话说,在另一次迭代中迭代它就足够了



我希望你明白我的意思
This is not how CP usually works. Most important goal here is to learn and help learning.
You are supposed to try it on your own, and come here when you got stuck with something, with a concrete question about your code, design, etc.
Please have a look to What have you tried?[^] to see a good explanation about what I mean.
Don't forget people here don't get payed. And besides, if we give you a ready-to-go solution, it is not going to help you because you are not going to learn anything from it.

EDIT (after comment of the algorithm):
I would say there are several points that could be important to choose the best solution. Example: the possible (max) lenght of the input string.

Considering the example you have given, I think it can be done easier.
- Take the array with the list of all reserved words in c#
- Iteration to go through the array, using IndexOf (link_1[^], link_2[^]) to check if the current reserved word is in the string.
If found: move to that index, counter++ and execute IndexOf again
If not: check next word

In other words, with an iteration within the other iteration it could be enough

I hope you understand what I mean


这篇关于如何编写一个程序,接受来自用户的字符串,对其进行标记,找到只有c#保留字的出现并给出单词的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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