Here you can find the source of countChar(String s, char c)
public static int countChar(String s, char c)
//package com.java2s; /******************************************************************************* * Copyright (c) 2014, Superloup10//from w ww . java 2 s . c om * * This work is made available under the terms of the GNU GENERAL PUBLIC LICENSE: * http://www.gnu.org/licenses/gpl-3.0.en.html * Contact the author for use the sources * * Cette ?uvre est mise ? disposition selon les termes de la GNU GENERAL PUBLIC LICENSE: * http://www.gnu.org/licenses/gpl-3.0.fr.html * Contacter l'auteur pour utiliser les sources ******************************************************************************/ public class Main { public static int countChar(String s, char c) { System.out.println("C:" + c); int n = 0; for (char c1 : s.toCharArray()) { System.out.println("C1:" + c1); if (c1 == c) { n++; System.out.println("Ntres" + n); } System.out.println("Nbis:" + n); } return n; } }