我也是刚学习,下面是我写的,希望能帮到你。
string zi = "传智播客欢迎您,欢迎到传智播客学习";
char[] num = zi.ToArray(); //转换成char类型的数组
int[] chishu = new int[num.Length];//用来储存每个字符出现的次数
for (int a = 0; a < num.Length; a++)
{
for (int b = a; b < num.Length; b++)
{
if (num[a] == num[b] && a != b)
{
chishu[a] = chishu[a] + 1;
num[b] = '!';//把已经比较的字做个标记
}
}
}
for (int i = 0; i < num.Length; i++)
{
if (num[i] != '!')
{
Console.WriteLine("{0}{1}", num[i], chishu[i] + 1);
}
} |