Sunday, 15 December 2013

Get character count of each character in a string

public class CharInArray {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        int current=0,highest=0;
        char highest_occuring_char=' ';
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String InputString=br.readLine();
        String alphabet="abcdefghijklmnopqrstuvwxyz";
        for(int i=0;i<26;i++){
            for(int j=0;j<InputString.length();j++){
                if(InputString.charAt(j)==alphabet.charAt(i))
                    current++;
            }
            if(current>highest){
                highest=current;
                highest_occuring_char=alphabet.charAt(i);
            }
            current=0;
        }
        System.out.println("The Highest occuring character is "+ highest_occuring_char);
     
    }
}

No comments:

Post a Comment