Program to count occurrence of a given character in a string

Given a string S and a character ‘c’, the task is to count the occurrence of the given character in the string.

Examples:  

Input : S = “CodeConfig” and c = ‘o’
Output : 2
Explanation: ‘o’ appears two times in str.

Input : S = “abccdefgaa” and c = ‘a’
Output : 3
Explanation: ‘a’ appears three times in str.

Count occurrence of a given character in a string using simple Iteration:

Iterate through the string and when each iteration, check if the current character is equal to the given character c then increments the count variable which stores count of the occurrences of the given character c in the string.

Below is the implementation of above approach:


Python3

# 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= "CodeConfig"
c = 'e'
print(count(str, c))
    
Output
4

Count occurrence of a given character in a string using Inbuild Functions:

The idea is to use inbuild functions/method in distinct programming languages which returns the count of occurrences of a character in a string.

Below is the implementation of above approach:

Python3

# Python program to count occurrences of
# a character using library
# Driver code to test above function
str = "CodeConfig"
c = 'e'
# Count returns number of occurrences of
# c between two given positions provided
# when two iterators.
print(len(str.split(c)) - 1);
Output
4

Count occurrence of a given character in a string using Recursion

Use a recursive approach to count the occurrences of a given character in a string. Checks if the character at the current index matches the target character, increments the count if it does, and then makes a recursive call to check the remaining part of the string. The process continues until the end of the string is reached, and the accumulated count would be the result.

Below is the implementation of above approach:


Python3

def count_in_string(ch, idx, s):
    # Base case: if the index reaches the end of the string, return 0
    if idx == len(s):
        return 0
    count = 0
    # Check if the current character of the string matches the given character
    if s[idx] == ch:
        count += 1
    # Recursively count occurrences in the remaining part of the string
    count += count_in_string(ch, idx + 1, s)
    return count
if __name__ == "__main__":
    str = "CodeConfig"
    c = 'e'
    print(count_in_string(c, 0, str))
Output
4

Explore More

Python 10 Programs for Beginners

Program 1:Write a program to create a 2D array using NumPy. Output: Program 2: Write a program to convert a python list to a NumPy array. Output: Program 3: Write a program to create matrix of 3×3 from 11 to 30. Output: Program 4: Write a program to create a data frame to store data of candidates who appeared in

Find index of first occurrence when an unsorted array is sorted

Find index of first occurrence when an unsorted an array is sorted Given an unsorted an array and a number x, find an index of first occurrence of x when

Find sum non repeating distinct elements array

Find sum of non-repeating (distinct) elements in an array Given an integer an array with repeated elements, the task is to find the sum of all distinct elements in the