Java - Write code to get Char Count of a string using for loop and String.charAt() method

Requirements

Write code to get Char Count

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String value = "book2s.com";
        char c = 'o';
        System.out.println(getCharCount(value, c));
    }/* w  ww  .  jav a2s . co  m*/

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

Related Example