Program to count occurrence of a given character in a string

Given a string and a character, 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 string
int 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 code
int main()
{
    string str= "geeksforgeeks";
    char c = 'e';
    cout << count(str, c) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to count occurrences
# of a given character
  
# Method that return count of the 
# given character in the string
def 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 code
str= "geeksforgeeks"
c = 'e'
print(count(str, c))
      
# This code is contributed by "rishabh_jain".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count occurrences
// of a character
using 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count 
// occurrences of a given
// character
  
// Function that return count of
// the given character in the string
function countt($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 countt($str, $c) ;
    return 0;
      
// This code is contributed by nitin mittal.
?>

chevron_right



Output:

4

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 contribute@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.



My Personal Notes arrow_drop_up

Improved By : Sam007, nitin mittal



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.