zoukankan      html  css  js  c++  java
  • 要求查询“"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();
  • 相关阅读:
    nodejs
    socket.io
    how to develop mobile web
    nodejs
    GitHub
    angularJS vs backbone
    AngularJS
    oracle 数据库中数据导出到excel
    cocos2d-x中的二段构造模式
    HDOJ 4975 A simple Gaussian elimination problem.
  • 原文地址:https://www.cnblogs.com/zhanying/p/3393533.html
Copyright © 2011-2022 走看看