注意可能很长的字符哦也不是全是字母 考虑下算法效率哦
输入:aaacccbbb33322145@#$输出:
a 3
c 3
b 3
3 3
2 2

解决方案 »

  1.   

    string GetCharCount(string text)
    {
    string result=string.Empty;
    int len=text.Length;
    while(len>0)
    {
    string temp=text[0].ToString();
    text=text.Replace(temp,"");
    int count=len-text.Length;
    result+=temp+","+count+"\r\n";
    len=text.Length;
    }
    return result;
    }
      

  2.   

    using System;
    using System.Collections.Generic;class A 

      static void Main()
      {
        string s = "aaacccbbb33322145@#$";
        Dictionary<char,int> count = GetCharCount(s);
        foreach (KeyValuePair<char,int> kvp in count)
        {
          Console.WriteLine("{0}:  {1}", kvp.Key, kvp.Value);
        }
      }
      
      static Dictionary<char,int> GetCharCount(string text)
      {
        Dictionary<char,int> d = new Dictionary<char,int>();
        foreach (char c in text)
        {
          if (d.ContainsKey(c)) d[c]++;
          else d[c] = 1;
        }
        return d;
      }
    }
    /* 程序输出:
    a:  3
    c:  3
    b:  3
    3:  3
    2:  2
    1:  1
    4:  1
    5:  1
    @:  1
    #:  1
    $:  1
    */
      

  3.   

    using System;
    using System.Collections.Generic;class A 

      static void Main()
      {
        string s = "aaacccbbb33322145@#$";
        Dictionary<char,int> count = GetCharCount(s);
        foreach (KeyValuePair<char,int> kvp in count)
        {
          if (kvp.Value > 1) Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
        }
      }
      
      static Dictionary<char,int> GetCharCount(string text)
      {
        Dictionary<char,int> d = new Dictionary<char,int>();
        foreach (char c in text)
        {
          int n;
          d.TryGetValue(c, out n);
          d[c] = n + 1;
        }
        return d;
      }
    }
    /* 程序输出:
    a:  3
    c:  3
    b:  3
    3:  3
    2:  2
    */
      

  4.   

    学这个吧:
    http://www.soasp.net/CShape/
      

  5.   

    学这个吧:
    http://www.soasp.net/CShape/