Count Occurrences of a Character in a String in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include<stdio.h>
#include<string.h>

int main() {
    char s[100], ch; int c=0;
    gets(s); ch = getchar();
    for(int i=0; s[i]; i++) if(s[i]==ch) c++;
    printf("%d", c);
}

C Output

Input:  
banana  
a  

Output:  
3


C++ Program

#include<iostream>
using namespace std;

int main() {
    string s; char ch; int c=0;
    getline(cin, s); cin >> ch;
    for(char x : s) if(x == ch) c++;
    cout << c;
}

C++ Output

Input:  
hello world  
l  

Output:  
3


JAVA Program

import java.util.*;

class O {
    public static void main(String[] a) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char ch = sc.next().charAt(0);
        int c = 0;
        for(char x : s.toCharArray()) if(x == ch) c++;
        System.out.print(c);
    }
}

JAVA Output

Input:  
programming  
g  

Output:  
2


Python Program

s = input()
ch = input()
print(s.count(ch))

Python Output

Input:  
mississippi  
s  

Output:  
4


In-Depth Learning – Whole Concept in Paragraphs
What Is "Count Occurrences of Character"?
Counting the occurrences of a character in a string involves counting how many times a given letter or symbol occurs within a word or sentence. For example, in the string "banana", the character 'a' occurs 3 times. This task is amongst the most basic operations within string processing.

How the Code Works
In every four languages, we:

Accept a string and a character as arguments.

Walk through the string.

Check each character in the string with the target character.

Increment a counter whenever they are equal.

At last, print the count.

In C/C++/Java, we use a loop and a counter. In Python, we can directly use the .count() method, which takes care of the traversal and counting for us.

Example
Input:


String: programming
Character: g

Output:

2

Explanation:

'g' is present at index 3 and 10 (0-based).

Total = 2 times

Real-Life Analogy
Suppose you're keeping track of the number of red balls in a basket full of balls of different colors. You examine each ball separately, and as you encounter a red ball, you add one to your counter.

Likewise, here, you're iterating over all characters of a word and checking how many times your desired character occurs. This is precisely how search engines, text editors, and even chat filters work to find certain patterns or spam characters.

Where and When Is It Used?
This kind of problem is widely employed in:

Search engines (e.g., find out how many times a keyword occurs)

Spell-checking tools

Plagiarism detection

Interview questions

Competitive programming

Bioinformatics (e.g., find DNA/RNA nucleotides count)

It's a building block for more complicated string operations such as pattern matching, regular expressions, and cleaning data.

Time and Space Complexity
Operation Complexity
Time O(n)
Space O(1)

You simply need to examine every character once. Space doesn't change since you're only utilizing a counter.

Python-Specific Tip
Python provides an inbuilt function for this:

python

s.count(ch)
It returns the frequency of ch in s, without any loops required. It's efficient and tidy — perfect for learners and experts alike.

SEO-Optimized Natural Paragraph for Ranking
Want to know how many times a character occurs in a string in C, C++, Java, or Python? This tutorial gives the most compact and efficient code for counting occurrences of a character in a string. Counting a specific character is one of the basic operations in text processing, interviews, and competitive programming. Whether you're developing a word processor, parsing user input, or examining string patterns, mastering the ability to perform character counting will refine your reasoning and give you a better grasp of loops, conditionals, and string-walking strategies. This entry-level tutorial features concrete examples and streamlined code for every prominent programming language.