Program to count occurrence of a given character in a string

1.4

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++

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

Java

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

C

// C program to count occurrences of a given character
#include<stdio.h>
#include<string.h>

// Function that return count of the given character
// in the string
int count(char *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()
{
    char str[]= "geeksforgeeks";
    char c = 'e';
    int result = count(str, c);
    printf("%d\n", count(str, c));
    return 0;
}


Output:

4

Using Direct Function in C++

// CPP program to count occurrences of
// a character using library
#include<bits/stdc++.h>
using namespace std;

// Driver code
int 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

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.

GATE CS Corner    Company Wise Coding Practice

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

Recommended Posts:



1.4 Average Difficulty : 1.4/5.0
Based on 17 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.