Find duplicate characters in a String and count the number of occurances using Java

前端 未结 30 2539
遇见更好的自我
遇见更好的自我 2020-12-14 11:47

How can I find the number of occurrences of a character in a string?

For example: The quick brown fox jumped over the lazy dog.

Some example

30条回答
  •  猫巷女王i
    2020-12-14 12:45

    public static void main(String args[]) {
        char Char;
        int count;
        String a = "Hi my name is Rahul";
        a = a.toLowerCase();
        for (Char = 'a'; Char <= 'z'; Char++) {
            count = 0;
            for (int i = 0; i < a.length(); i++) {
                if (a.charAt(i) == Char) {
                    count++;
                }
            }
            System.out.println("Number of occurences of " + Char + " is " + count);
        }
    }
    

提交回复
热议问题