Strings are a very important topic in programming interviews. It’s wise to practice some programming problems focused on strings before your interviews. In this article, you’ll learn how to count the total occurrences of a character in a string.
Examples to Understand the Problem
Example 1: Let the given string be “she sells seashells by the seashore” and the given character be ‘s’.
str = “she sells seashells by the seashore”
ch = ‘s’
There are eight occurrences of the character s in the given string.
Thus, the output is 8.
Example 2: Let the given string be “He threw three free throws” and the given character be ‘e’.
str = “He threw three free throws”
ch = ‘e’
There are six occurrences of the character e in the given string.
Thus, the output is 6.
Approach to Count the Total Occurrences of a Character in a String
You can find the count of total occurrences of a character in a string by following the approach below:
- Initialize a counter variable to store the count of total occurrences of a character in a string.
- Traverse the string character by character.
- If the character of the string matches with the given character, increment the value of the count variable.
- Finally, return the counter variable.
C++ Program to Count the Total Occurrences of a Character in a String
Below is the C++ program to count the total occurrences of a character in a string:
// C++ program to count occurrences
// of a given character in a string
#include
#include
using namespace std;
// Function to count the occurrences of
// the given character in the string
int countOccurrences(string str, char ch)
{
// Counter variable
int counter = 0;
for (int i = 0; i {
// check if the given character matches
// with the character in the string
if (str[i] == ch)
{
// if the given character matches with
// the character in the string,
// increment the counter variable
counter++;
}
}
return counter;
}
// Driver code
int main()
{
string str1 = "she sells seashells by the seashore";
char ch1 = 's';
cout cout countOccurrences(str1, ch1) string str2 = "peter piper picked a peck of pickled peppers";
char ch2 = 'p';
cout cout countOccurrences(str2, ch2) string str3 = "I saw Susie sitting in a shoeshine shop";
char ch3 = 'a';
cout cout countOccurrences(str3, ch3) string str4 = "Near an ear, a nearer ear, a nearly eerie ear";
char ch4 = 'r';
cout cout countOccurrences(str4, ch4) string str5 = "He threw three free throws";
char ch5 = 'e';
cout cout countOccurrences(str5, ch5) return 0;
}
Output:
Input string 1: she sells seashells by the seashore
Character s has occurred 8 times in the given string.
Input string 2: peter piper picked a peck of pickled peppers
Character p has occurred 9 times in the given string.
Input string 3: I saw Susie sitting in a shoeshine shop
Character a has occurred 2 times in the given string.
Input string 4: Near an ear, a nearer ear, a nearly eerie ear
Character r has occurred 8 times in the given string.
Input string 5: He threw three free throws
Character e has occurred 6 times in the given string.
Python Program to Count the Total Occurrences of a Character in a String
Below is the Python program to count the total occurrences of a character in a string:
# Python program to count occurrences
# of a given character in a string
# Function to count the occurrences of
# the given character in the string
def countOccurrences(str, ch):
# Counter variable
counter = 0
for char in str:
# check if the given character matches
# with the character in the string
if char == ch:
# if the given character matches with
# the character in the string,
# increment the counter variable
counter += 1
return counter
# Driver code
str1 = "she sells seashells by the seashore"
ch1 = 's'
print("Input string 1:", str1)
print("Character", ch1, "has occured",
countOccurrences(str1, ch1),"times in the given string.")
str2 = "peter piper picked a peck of pickled peppers"
ch2 = 'p'
print("Input string 2:", str2)
print("Character", ch2, "has occured",
countOccurrences(str2, ch2),"times in the given string.")
str3 = "I saw Susie sitting in a shoeshine shop"
ch3 = 'a'
print("Input string 3:", str3)
print("Character", ch3, "has occured",
countOccurrences(str3, ch3),"times in the given string.")
str4 = "Near an ear, a nearer ear, a nearly eerie ear"
ch4 = 'r'
print("Input string 4:", str4)
print("Character", ch4, "has occured",
countOccurrences(str4, ch4),"times in the given string.")
str5 = "He threw three free throws"
ch5 = 'e'
print("Input string 5:", str5)
print("Character", ch5, "has occured",
countOccurrences(str5, ch5),"times in the given string.")
Output:
Input string 1: she sells seashells by the seashore
Character s has occurred 8 times in the given string.
Input string 2: peter piper picked a peck of pickled peppers
Character p has occurred 9 times in the given string.
Input string 3: I saw Susie sitting in a shoeshine shop
Character a has occurred 2 times in the given string.
Input string 4: Near an ear, a nearer ear, a nearly eerie ear
Character r has occurred 8 times in the given string.
Input string 5: He threw three free throws
Character e has occurred 6 times in the given string.
JavaScript Program to Count the Total Occurrences of a Character in a String
Below is the JavaScript program to count the total occurrences of a character in a string:
// JavaScript program to count occurrences
// of a given character in a string
// Function to count the occurrences of
// the given character in the string
function countOccurrences(str, ch)
{
// Counter variable
var counter = 0;
for (let i = 0; i {
// check if the given character matches
// with the character in the string
if (str[i] == ch)
{
// if the given character matches with
// the character in the string,
// increment the counter variable
counter++;
}
}
return counter;
}
// Driver code
var str1 = "she sells seashells by the seashore";
var ch1 = 's';
document.write("Input string 1: " + str1 + "
");
document.write("Character " + ch1 + " has occurred " +
countOccurrences(str1, ch1) + " times in the given string." + "
");
var str2 = "peter piper picked a peck of pickled peppers";
var ch2 = 'p';
document.write("Input string 2: " + str2 + "
");
document.write("Character " + ch2 + " has occurred " +
countOccurrences(str2, ch2) + " times in the given string." + "
");
var str3 = "I saw Susie sitting in a shoeshine shop";
var ch3 = 'a';
document.write("Input string 3: " + str3 + "
");
document.write("Character " + ch3 + " has occurred " +
countOccurrences(str3, ch3) + " times in the given string." + "
");
var str4 = "Near an ear, a nearer ear, a nearly eerie ear";
var ch4 = 'r';
document.write("Input string 4: " + str4 + "
");
document.write("Character " + ch4 + " has occurred " +
countOccurrences(str4, ch4) + " times in the given string." + "
");
var str5 = "He threw three free throws";
var ch5 = 'e';
document.write("Input string 5: " + str5 + "
");
document.write("Character " + ch5 + " has occurred " +
countOccurrences(str5, ch5) + " times in the given string." + "
");
Output:
Input string 1: she sells seashells by the seashore
Character s has occurred 8 times in the given string.
Input string 2: peter piper picked a peck of pickled peppers
Character p has occurred 9 times in the given string.
Input string 3: I saw Susie sitting in a shoeshine shop
Character a has occurred 2 times in the given string.
Input string 4: Near an ear, a nearer ear, a nearly eerie ear
Character r has occurred 8 times in the given string.
Input string 5: He threw three free throws
Character e has occurred 6 times in the given string.
Other Methods to Solve the Problem
You can find the count of total occurrences of a character in a string by other methods like recursion, regular expressions, library functions, etc. The iterative method used in this article is one of the simplest methods to solve this problem.
If you want to practice more problems on strings, check out problems like how to reverse a string, how to check if a string is a palindrome, how to find the total count of vowels, consonants, digits, and special characters in a string, etc.