A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄冉 中级黑马   /  2012-11-20 07:53  /  1322 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. <p>using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;</p><p>namespace test9
  6. {
  7.     class Program
  8.     {
  9.       </p><p>
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("输入一段字符串:");
  13.             string str = Console.ReadLine();
  14.             str = str.ToLower();
  15.             Dictionary<char,int> dic=new Dictionary<char,int>();
  16.             for(int i=0;i<str.Length;i++)
  17.             {
  18.                 if (!dic.ContainsKey(str[i]))//如果字典中没有这个字符则将这个字符加入字典中
  19.                 {
  20.                     dic.Add(str[i], 1);
  21.                 }
  22.                 else//如果字典中存在这个字符,则个数+1
  23.                 {
  24.                     dic[str[i]]++;
  25.                 }
  26.             }
  27.             foreach (KeyValuePair<char,int> kv in dic)
  28.             {
  29.                 Console.WriteLine("字符{0}出现{1}次", kv.Key, kv.Value);
  30.             }
  31.             Console.ReadKey();
  32.         }
  33.     }
  34. }</p>
复制代码

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

2 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
  1. //找出一句话中字母“e”出现的个数
  2.             string strs = Console.ReadLine();

  3.             //方法一
  4.             string[] words = strs.Split(new char[] {'e'}, StringSplitOptions.RemoveEmptyEntries);

  5.             Console.WriteLine("您刚才输入的话中字母“e”出现的次数为:{0}",words.Length-1);
  6.             Console.WriteLine("每次出现的位置分别为:");

  7.             int pos = 0;
  8.             for (int i = 0; i < words.Length-1; i++)
  9.             {
  10.                 pos += words[i].Length+1;

  11.                 Console.WriteLine("第{0}次出现的位置为{1}",i+1,pos);
  12.             }

  13.             //方法二
  14.             int posi = 0,count = 0;
  15.             while (posi <= strs.LastIndexOf('e'))
  16.             {
  17.                 if (strs.IndexOf('e') > 0)
  18.                 {
  19.                     posi = strs.IndexOf('e', posi) + 1;
  20.                     count++;
  21.                 }
  22.                 Console.WriteLine("第{0}此出现的位置为:{1}",count,posi);
  23.             }

  24.             Console.WriteLine("一共出现了{0}次", count);
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马