在c#中编写程序来计算字符串中数字的出现而不进行排序? [英] Write a program in c# to count occurrence of digit in a string without sorting?
本文介绍了在c#中编写程序来计算字符串中数字的出现而不进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用c#编写程序来计算字符串中数字的出现而不进行排序?
示例:
如果输入字符串是012311234567712
输出应该是这样的: -
0:*
1:****
2:***
3:**
4:*
5:*
6:*
7:**
8:
9:
Write a program in c# to count occurrence of digit in a string without sorting?
Example:
if input string is "012311234567712"
Output should be like this:-
0:*
1:****
2:***
3:**
4:*
5:*
6:*
7:**
8:
9:
推荐答案
string input = "012311234567712";
int[] result = new int[10];
foreach (char c in input.Where(char.IsDigit))
++result[c - '0'];
for (int i = 0; i < result.Length; ++i)
Console.WriteLine("{0}:{1}", i, new string('*', result[i]));
创建一个包含10个整数的数组,表示数字0到9,并将all设置为零。
迭代遍历每个字符字符串并增加相应的数组元素。
make an array of 10 integers representing the digits 0 to 9 and set all to zero.
the iterate through each character in the string and increment the corresponding array element.
这篇关于在c#中编写程序来计算字符串中数字的出现而不进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文