- Console.WriteLine("请输入要检测的字符串");
- string str = Console.ReadLine();
- string strNoRepeat = string.Empty;
- for (int i = 0; i < str.Length; i++) //去掉字符串中重复出现的字符
- {
- //当新字符串已包含第i个字符时,直接进入下一次循环
- if (strNoRepeat.Contains(str[i].ToString()))
- {
- continue;
- }
- strNoRepeat += str[i];
- }
- int[] times=new int[strNoRepeat.Length]; //用来保存每个字符出现的次数
- for (int i = 0; i < strNoRepeat.Length; i++)
- {
- for (int j = 0; j < str.Length; j++)
- {
- if (strNoRepeat[i] == str[j])
- {
- times[i]++; //当字符重复出现时次数加1
- }
- }
- Console.WriteLine();
- Console.Write("{0}出现{1}次\t", strNoRepeat[i], times[i]);
- }
- int timeRepeat = 0; //统计连续出现的次数
- //当第i个字符等于第i-1个时,连续出现次数加1;
- //当不相等时,判断连续出现次数是否大于1,大于1就输出统计的次数,并将次数清零
- for (int i = 1; i < str.Length; i++)
- {
- if (str[i] == str[i - 1])
- {
- timeRepeat++;
- }
- else if (timeRepeat >= 1)
- {
- Console.WriteLine("{0}连续出现{1}个",str[i-1],timeRepeat+1);
- timeRepeat = 0;
- }
- }
- Console.ReadKey();
复制代码 小白思路,不要见笑。。。 |