- //找出一句话中字母“e”出现的个数
- string strs = Console.ReadLine();
- //方法一
- string[] words = strs.Split(new char[] {'e'}, StringSplitOptions.RemoveEmptyEntries);
- Console.WriteLine("您刚才输入的话中字母“e”出现的次数为:{0}",words.Length-1);
- Console.WriteLine("每次出现的位置分别为:");
- int pos = 0;
- for (int i = 0; i < words.Length-1; i++)
- {
- pos += words[i].Length+1;
- Console.WriteLine("第{0}次出现的位置为{1}",i+1,pos);
- }
- //方法二
- int posi = 0,count = 0;
- while (posi <= strs.LastIndexOf('e'))
- {
- if (strs.IndexOf('e') > 0)
- {
- posi = strs.IndexOf('e', posi) + 1;
- count++;
- }
- Console.WriteLine("第{0}此出现的位置为:{1}",count,posi);
- }
- Console.WriteLine("一共出现了{0}次", count);
复制代码 |