(1)
static void Main(string[] args) { string a = "abcdefgabc"; int i = a.Length; while (i > 0) { Console.WriteLine(a[0].ToString() + ":"); a = a.Replace(a[0].ToString(), ""); Console.Write(i - a.Length); Console.WriteLine(); i = a.Length; } }
(2)
static void Main(string[] args) { Console.WriteLine("请输入字符串:"); string str = Console.ReadLine(); CharCount(str); } public static void CharCount(string str) { if (str == string.Empty) return; char ch = str.Substring(0).ToCharArray()[0]; int num = str.Split(ch).Length - 1; Console.WriteLine("char: {0} num: {1}", ch, num); CharCount(str.Replace(ch.ToString(), "")); }
(3)
static void Main(string[] args) { string s = "abcdefgabc"; Dictionary<char, int> list = new Dictionary<char, int>(); for (int i = 0; i < s.Length; i++) { if (list.ContainsKey(s[i])) { list[s[i]]++; } else { list.Add(s[i], 1); } } foreach (char key in list.Keys) { Console.WriteLine("{0}:{1}", key, list[key]); } }