/*取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,
* 输出格式为:a(2)b(1)k(2)...*/
String str = "abcdekka27qoq";
char[] ch1 = str.toCharArray();
for (int i = 0; i < ch1.length - 1; i++) {
for (int j = i + 1; j < ch1.length; j++) {
if(ch1[i] > ch1[j]){
int temp = ch1[i];
ch1[i] = ch1[j];
ch1[j] = (char) temp;
}
}
}
System.out.println(Arrays.toString(ch1));
for (int i = 0; i < ch1.length; ) {
int count = 0;
for (int j = i + 1; j < ch1.length; j++) {
if(ch1[i] == ch1[j]) {
count++;
}
}
System.out.print(ch1[i]+"("+(count + 1)+")");
i = i + count;
i++;
}
|