
Hi,
can any one please share the code to find the occurance of characters in a string??
ex:- aaabb o/p: a=3 b=2

Here is an example that count accepts the string from the user and count the occurrence of each character in the string.
import java.io.*;
class CountCharacters {
public static void main(String[] args) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter string ");
System.out.println();
String str=br.readLine();
String st=str.replaceAll(" ", "");
char[]third =st.toCharArray();
for(int counter =0;counter<third.length;counter++){
char ch= third[counter];
int count=0;
for ( int i=0; i<third.length; i++){
if (ch==third[i])
count++;
}
boolean flag=false;
for(int j=counter-1;j>=0;j--){
if(ch==third[j])
flag=true;
}
if(!flag){
System.out.println("Character :"+ch+" = "+count);
}
}
}
}