正则表达式

又一次失败的笔试,我很无奈。回家查了下资料,参考正则表达式API实现。

题目:查找一个字符串中出现次数最多的字符

我用循环,计算每个字符的次数放入map中,但这显然不是他们的思想,悲剧。用正则实现如下:
public void exercise2()
	{
		String s = "you are still that childish.hahaha,aoaoaoaaaa";
		int k = 0;
		char c = (char)'a';
		while(!s.equals(""))
		{
			System.out.println(s);
			Pattern p = Pattern.compile(s.charAt(0)+"");
			Matcher m = p.matcher(s);
			int j = 0;
			while(m.find())
				j++;
			if(j>k)
			{
				k = j;
				c=s.charAt(0);
			}
			s = m.replaceAll("");
		}
		System.out.println("char "+c+" occurs "+k+" times");
	}

你可能感兴趣的:(C++,c,正则表达式,C#,J#)