C Program to Count the Number of Occurrences of a Character in a String

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

The total number of occurrences of a character in a string can be calculated by iterating through the string and comparing each character with the character whose occurrence we are counting and increasing the count when the character matches. In this article, we will learn how to write a c program to count the number of occurrences of a character in a string.

Introduction

Suppose we are given a string and we are expected to tell about the number of occurrences of vowels thus we would return the sum of the number of occurrences of {a, e, i, o, u} in the string, or suppose we are given a string and we have to check if a character is present in it or not, thus we would see if the character is present in more than 0 times and return the result accordingly.

In the above cases, there is one thing in common, we are supposed to deal with the number of occurrences of a particular character or characters in a string.

In programming, we often encounter situations where we have to tell the frequency of a character in a string. In the C language, there are many ways to obtain the character count in the given string. We can obtain the character count by looping through the string and comparing each character with our desired character (the character whose occurrence is to be counted).

In this article we will learn intuition as well as a depth explanation of how to write a c program to count the number of occurrences of a character in a string

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

C program to Count the Number of Occurrences of a Character in a String

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Using Standard Method

Algorithm:

  • Step 1: We will declare a cnt variable of integer type and assign it a value 0. This variable will keep the count of the occurrence of the character we are counting in the given string.
  • **Step 2: **We will iterate through the string. On each iteration, we will compare the current character with the character whose occurrence we are counting in the given string. If the if condition is true then we will increment the count by 1.
  • **Step 3: **Once the iteration is completed we will output the cnt variable.

Input:

Output:

Explanation of the example:

In the above example, we are counting the occurrence of character i in mississippi. Thus the c program to count the number of occurrences of a character in a string will go through each character of the string and compare it with i and in case it matches then it will increment cnt. Thus the final result is 4.

Using Function

Algorithm:

  • Step 1: We will declare a cnt variable of integer type and assign it a value 0. This variable will keep the count of the occurrence of the character we are counting in the given string.
  • Step 2: We will iterate through the string. On each iteration, we will compare the current character with the character whose occurrence we are counting in the given string. If the if condition is true then we will increment the count by 1.
  • Step 3: Once the iteration is completed we will output the cnt variable.

Code:

Input:

Output:

Explanation of the example:

In the above example, we are counting the occurrence of character e in Peter. Thus the c program to count the number of occurrences of a character in a string will go through each character of the string and compare it with i and in case it matches then it will increment cnt. Thus the final result is 2.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Using Recursion

Algorithm:

  • Step 1: We will declare a cnt variable of integer type and an i variable of integer type and assign them a value 0. The cnt variable will keep the count of the occurrence of the character we are counting in the given string and the i variable will refer to the current index of the string. Both cnt and i are declared static since their scope needs to be global and their value should be maintained upon each function call.
  • Step 2: We will compare if str[i] is 0. This means that the iteration is completed, in that case, we will return the cnt.
  • Step 3: If the if condition returns false then we will compare the current character with the character whose occurrence we are counting in the given string. If the inner if condition is true then we will increment the count by 1.
  • Step 4: We will increment the value of i and call the function countNoOfOccurence recursively.

Code:

Input:

Output:

Explanation of the example:

In the above example, we are counting the occurrence of the character a in business. Thus the c program to count the number of occurrences of a character in a string will go through each character of the string and compare it with i and in case it matches then it will increment cnt. Since there is no occurrence of a in business thus the final result is 0.

Conclusion

  • The occurrence of a character in a string in c is counted by looping through the string and comparing each character.
  • The main idea behind a c program to count the number of occurrences of a character in a string is to keep count of the appearance of the character.
  • We can write a c program to count the number of occurrences of a character in a string either by the standard method or with the use of conventional as well as recursive functions.