Program to count occurrence of a given character in a string

25 May, 2022
Given a string and a character, the task is to make a function which count occurrence of the given character in the string. 
Examples:  
Input : str = "geeksforgeeks"
         c = 'e'
Output : 4
'e' appears four times in str.

Input : str = "abccdefgaa"
          c = 'a' 
Output : 3
'a' appears three times in str.
C++
Java
Python3
C#
PHP
Javascript
// C++ program to count occurrences of a given// character#include <iostream>#include <string>using namespace std; // Function that return count of the given// character in the stringint count(string s, char c){    // Count variable    int res = 0;     for (int i=0;i<s.length();i++)         // checking character in string        if (s[i] == c)            res++;     return res;} // Driver codeint main(){    string str= "geeksforgeeks";    char c = 'e';    cout << count(str, c) << endl;    return 0;}
// JAVA program to count occurrences// of a character class GFG{    // Method that return count of the given    // character in the string    public static int count(String s, char c)    {        int res = 0;         for (int i=0; i<s.length(); i++)        {            // checking character in string            if (s.charAt(i) == c)            res++;        }        return res;    }         // Driver method    public static void main(String args[])    {        String str= "geeksforgeeks";        char c = 'e';        System.out.println(count(str, c));    }}
# Python program to count occurrences# of a given character # Method that return count of the# given character in the stringdef count(s, c) :         # Count variable    res = 0         for i in range(len(s)) :                 # Checking character in string        if (s[i] == c):            res = res + 1    return res          # Driver codestr= "geeksforgeeks"c = 'e'print(count(str, c))     # This code is contributed by "rishabh_jain".
// C# program to count occurrences// of a characterusing System;         public class GFG {         // Method that return count of the given    // character in the string    public static int count(string s, char c)    {        int res = 0;         for (int i = 0; i < s.Length; i++)        {                         // checking character in string            if (s[i] == c)            res++;        }                 return res;    }         // Driver method    public static void Main()    {        string str = "geeksforgeeks";        char c = 'e';                 Console.WriteLine(count(str, c));    }} // This code is contributed by Sam007.
<?php// PHP program to count// occurrences of a given// character // Function that return count of// the given character in the stringfunction count($s, $c){         // Count variable    $res = 0;     for ($i = 0; $i < strlen($s); $i++)         // checking character in string        if ($s[$i] == $c)            $res++;     return $res;}     // Driver Code    $str= "geeksforgeeks";    $c = 'e';    echo count($str, $c) ;    return 0;     // This code is contributed by nitin mittal.?>
<script>// JAVASCRIPT program to count occurrences// of a character    // Method that return count of the given    // character in the string    function count(s, c)    {        let res = 0;         for (let i = 0; i < s.length; i++)        {            // checking character in string            if (s.charAt(i) == c)            res++;        }        return res;    }         // Driver method          let str= "geeksforgeeks";        let c = 'e';        document.write(count(str, c));          // This code is contributed by shivanisinghss2110       </script>
4
Time Complexity: O(len), where len is the size of the string given.
Auxiliary Space: O(1)Using Direct Function in C++ 
C++
// CPP program to count occurrences of// a character using library#include<bits/stdc++.h>using namespace std; // Driver codeint main(){    string str = "geeksforgeeks";    char c = 'e';       // Count returns number of occurrences of    // c between two given positions provided    // as two iterators.    cout << count(str.begin(), str.end(), c);    return 0;}
Output:  
4
Time Complexity: O(len), where len is the size of the string given.
Auxiliary Space: O(1) 
Using recursion
C++
Java
Python3
C#
Javascript
#include<bits/stdc++.h>using namespace std; int countinString(char ch,string s){    // base case;    if (s.length() == 0)        return 0;        int count = 0;     // checking if the first character of    // the given string is that character    // or not    if (s[0] == ch)        count++;         // this will count the occurrence of        // given character in the string        // from index 1 to the last        // index of the string    count += countinString(ch, s.substr(1));     return count;} int main(){    string str = "geeksforgeeks";    char c = 'e';    cout<<(countinString(c, str));} // This code is contributed by shinjanpatra
/*package whatever //do not write package name here */ import java.io.*; class GFG {      static int countinString(char ch, String s)    {          //base case;        if(s.length()==0)            return 0;          int count = 0;                 //checking if the first character of          //the given string is that character          //or not          if(s.charAt(0)==ch)          count++;                 //this will count the occurrence of          //given character in the string          //from index 1 to the last          //index of the string          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));    }}
def countinString(ch, s):     # base case;    if (len(s) == 0):        return 0    count = 0     # checking if the first character of    # the given string is that character    # or not    if (s[0] == ch):        count += 1     # this will count the occurrence of    # given character in the string    # from index 1 to the last    # index of the string    count += countinString(ch, s[1:])     return count     str = "geeksforgeeks"c = 'e'print(countinString(c, str)) # This code is contributed by shinjanpatra
/*package whatever //do not write package name here */using System;public class GFG {      static int countinString(char ch, String s)    {          // base case;        if(s.Length == 0)            return 0;          int count = 0;                 // checking if the first character of          // the given string is that character          // or not          if(s[0] == ch)          count++;                 // this will count the occurrence of          // given character in the string          // from index 1 to the last          // index of the string          count += countinString(ch,s.Substring(1));                          return count;    }     // Driver code    public static void Main(String[] args) {        String str= "geeksforgeeks";        char c = 'e';        Console.WriteLine(countinString(c,str));    }} // This code is contributed by umadevi9616
<script>/*package whatever //do not write package name here */     function countinString( ch, s)    {        // base case;        if (s.length == 0)            return 0;        var count = 0;         // checking if the first character of        // the given string is that character        // or not        if (s[0] == ch)            count++;         // this will count the occurrence of        // given character in the string        // from index 1 to the last        // index of the string        count += countinString(ch, s.substring(1));         return count;    }             var str = "geeksforgeeks";        var c = 'e';        document.write(countinString(c, str)); // This code is contributed by gauravrajput1</script>
Output:
4
Time Complexity: O(len), where len is the size of the string given.
Auxiliary Space: O(len), where len is the size of the string given.
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Sam007
nitin mittal
le0
sagar0719kumar
shivanisinghss2110
surindertarika1234
GauravRajput1
patildhanu4111999
shinjanpatra
rohitmishra051000
C++
Strings
Strings
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Priority Queue in C++ Standard Template Library (STL)
vector erase() and clear() in C++
Substring in C++
Sorting a vector in C++
2D Vector In C++ With User Defined Size
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Check for Balanced Brackets in an expression (well-formedness) using Stack