编程以查找字符串中字符的出现次数 [英] Program to find the ocurence of character in a string

查看:103
本文介绍了编程以查找字符串中字符的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想写一个能算出角色出现次数的程序。



示例:

输入字符串= NAMAN

输出将为:

N = 2

A = 2

M = 1



关于

Hi,

I want to write a program which is able to count the ocurence of character.

Example:
input string=NAMAN
Output will be:
N=2
A=2
M=1

Regards

推荐答案

看这里:

Look here:
var chars = "NAMAN";
chars.Distinct().ToList().ForEach(c => Console.WriteLine("{0}={1}", c, chars.Count(i => i == c)));





[更新:不需要ToCharArray()]


我使用解决方案3中的LINQ进行了显着改进:



I have a significant improvement using LINQ from solution 3:

str.ToCharArray().GroupBy(i => i).OrderBy(k => k.Key).ToList().
  ForEach(m => Console.WriteLine(string.Format("{0}: {1}", m.Key, m.Count())));


嗅,嗅......是的,闻起来像家庭作业!



对不起,但你得到的答案可能会让你在课堂上成绩不及格。根据你获得的任务水平,你的课程没有办法覆盖你在这里得到的答案所展示的方法,教授课程的人知道你没有写代码。
Sniff, sniff... Yep, smells like homework!

Sorry, but the answers you got will probably get you a failing grade in your class. With the level of the assignment you got, there''s no way your class covered the methods demonstrated in the answers you got here and the person teaching the class will KNOW you didn''t write the code.


这篇关于编程以查找字符串中字符的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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