编程以查找字符串中字符的出现次数

编程入门 行业动态 更新时间:2024-10-11 15:20:49
本文介绍了编程以查找字符串中字符的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想写一个能算出角色出现次数的程序。 示例: 输入字符串= 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.

更多推荐

编程以查找字符串中字符的出现次数

本文发布于:2023-11-30 13:15:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   次数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!