Java - Write code to count Occurrence of a char inside a string with for loop

Requirements

Write code to count Occurrence

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String matchedString = "book2s.com";
        char Occurrence = 'a';
        System.out.println(countOccurrence(matchedString, Occurrence));
    }/*from w w  w.java  2 s.c o m*/

    public static int countOccurrence(String matchedString, char Occurrence) {
        int count = 0;
        for (final char c : matchedString.toCharArray()) {
            if (c == Occurrence) {
                ++count;
            }
        }
        return count;
    }
}

Related Example