Java - Write code to count Matches for a char inside a string

Requirements

Write code to count Matches

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String haystack = "book2s.com";
        char c = 'a';
        System.out.println(countMatches(haystack, c));
    }//from  ww w .  ja  v a  2s  . co m

    public static int countMatches(String haystack, char c) {
        int count = 0;
        for (int i = 0; i < haystack.length(); i++) {
            if (haystack.charAt(i) == c)
                count++;
        }
        return count;
    }
}