我该如何计算C#中每个字母的数量? [英] How should I calculate the each letter's count in C#

查看:139
本文介绍了我该如何计算C#中每个字母的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我在文本框中输入文本作为代码项目(不仅代码项目文本是什么)



i希望输出像

c-2

o-2

d-1

e-2

p-1

r-1

j-1

t-1



如何在c中实现这个目标#



我尝试过:



i不知道如何做,请帮助代码谢谢

for example if i enter the text in textbox as 'Code Project' (not only code project what ever the text is)

i want the output as like
c-2
o-2
d-1
e-2
p-1
r-1
j-1
t-1

how to achieve this in c#

What I have tried:

i dont know how to do it please help with the code thanks

推荐答案

你将得不到任何代码 - 这是你的功课,所以由你来做它!



但如果你想一想,它就很简单。

首先将输入转换为全部小写(这很简单,就是这样)一个名为ToLower的字符串方法)现在创建两个数组,两个数组的大小与输入的字符串相同,以及一个表示使用量的整数。一个数组包含字符,另一个数组包含整数。 (如果您了解类,可以是包含整数和字符的类的列表,或者字典< char,int> - 但我会假设您没有这样做)

现在依次查看输入中的每个字符。

如果它在你的字符数组中,则增加相应整数数组中的值。

如果不是,请添加它到你的characters数组,将相应的整数数组元素设置为1,并增加使用多少变量。



循环后,你有字符和yoru数组中相应的计数,很容易打印出来。
You'll get no code - this is your homework so it's up to you to do it!

But if you think about it, it's pretty simple.
Start by converting the input to all lowercase (that's simple, there's a string method for that called ToLower) Now create two arrays, both the same size as the string he entered, and a integer that says how much is used. One array holds characters, the other holds integers. (If you know about classes, this can be a List of a class containing an integer and a character, or a Dictionary<char, int> - but I'll assume you don't)
Now look at each character in the input in turn.
If it's in your character array, increment the value in the corresponding integer array.
If it isn't, add it to your characters array, set the corresponding integer array element to 1, and increment the "how much is used" variable.

After the loop, you have the characters and corresponding counts in yoru arrays and it's easy to print them.


引用:

我不知道如何请帮助代码谢谢

i dont know how to do it please help with the code thanks



只是给你代码不会帮助你学习一些东西。

HomeWork问题是那种简化版本你必须在现实生活中解决的问题,他们的目的是学习和 pra cticing 的。




Just giving you the code will not help you to learn something.
HomeWork problems are simplified versions of the kind of problems you will have to solve in real life, their purpose is learning and practicing.

引用:

我希望输出像

c-2

o-2

d-1

e-2

p-1

r-1

j-1

t-1

i want the output as like
c-2
o-2
d-1
e-2
p-1
r-1
j-1
t-1



按字母顺序给出结果可能更容易编程。

您知道如何手动解决问题,以机械方式思考,这基本上就是您的算法。



我们不做你的家庭工作。

HomeWork不会在乞求别人做你的工作时测试你的技能,它会让你思考并帮助你的老师检查你的了解您所学的课程以及您应用这些课程时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。


Giving result in alphabetic order may be easier to program.
You know how to solve the problem by hand, think about it in a mechanical fashion, this is basically your algorithm.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.


你应该至少谷歌它



这里有一些例子。

统计所有角色String C#中出现的事件 - 堆栈溢出 [ ^ ]



https://www.dotnetperls.com/count-letter-frequencies [ ^ ]



CP_count_letter | C#在线编译器| .NET小提琴 [ ^ ]
You should at least Google it

Here are some examples.
Count All Character Occurrences in a String C# - Stack Overflow[^]

https://www.dotnetperls.com/count-letter-frequencies[^]

CP_count_letter | C# Online Compiler | .NET Fiddle[^]


这篇关于我该如何计算C#中每个字母的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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