How to Find Occurrence Of Character In String

In this example, we’ll find the occurrence of character in String:

Input:
abbbcdefg
b

Output:
Char 'b' occurs 3 times 

1. Java

public class FindOccurrenceOfGivenCharacterInString {
    public static void main(String[] args) {
        String value = "aaabbbcccddd";
        char givenChar = 'a';
        int occurrence = 0;
        char[] charArray = value.toCharArray();

        for (char c : charArray) {
            if (c == givenChar) {
                occurrence++;
            }

        }

        System.out.println("Char '" + givenChar + "' occurs " + occurrence + " times ");
    }
}

Output:

Char 'a' occurs 3 times 

Complete Java Solutions can be found here: Github Link

The complete list can be found here: Practise Programming Questions