要求查询“"Welcome to Chinaworld"“中重复的字符,不区分大小写,并且打印出来每个字符出现的次数

 static void Main(string[] args)
        {
            string s = "Welcome to Chinaworld";
            Dictionary<char,int> dict=new Dictionary<char,int>();//先弄一个字典,key用来放字母,值用来放出现次数
            //遍历字符串,区分大小写,全部转换小写
            foreach(char ch in s.ToLower())
            {
                //如果字符串中的每个字符都没在字典的dict.containskey()方法中匹配到,就else
                //把dict[ch]=1表示第一次出现;如果匹配到表示出现多次,dict[ch]就累加
                if(dict.ContainsKey(ch))
                {
                    dict[ch]++;
                }
                else
                {
                    dict[ch]=1;
                }
            }
            //遍历字典的dict.keys键的集合,输出键对应的值
            foreach(char ch in dict.Keys)
            {
               
                Console.WriteLine(ch+"         "+dict[ch].ToString());
                
            }
            Console.ReadKey();

 

posted @ 2013-10-29 01:11  南瓜asp  阅读(234)  评论(0编辑  收藏  举报