Program to count occurrence of a given character in a string | package whatever do not write package name here ; base case ; ; checking if the first character of the given string is that character or not ; this will count the occurrence of given character in the string from index 1 to the last index of the string ; Driver code
import java . io . * ; class GFG { static int countinString ( char ch , String s ) { if ( s . length ( ) == 0 ) return 0 ; int count = 0 ; if ( s . charAt ( 0 ) == ch ) count ++ ; count += countinString ( ch , s . substring ( 1 ) ) ; return count ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; char c = ' e ' ; System . out . println ( countinString ( c , str ) ) ; } }