Count Occurrences of a Character in String C++

  1. Use the Iterative Method to Count Occurrences of a Character In a String
  2. Use the std::tolower Function to Count Character Occurrences Regardless of Letter Case

This article will explain several methods of how to count occurrences of a character in a string C++.

Use the Iterative Method to Count Occurrences of a Character In a String

The C++ string library provides a std::string class, which can be used to store and manipulate char-like object sequences. It provides multiple methods to search and find the given character or a substring in the string, but in this case, we need to check every character so it would be simpler to implement a function that iterates through the string while incrementing the count variable. This function takes a string object as a reference and a char by value to count the occurrences. Mind though, this version does not distinguish the capital letters from the lower ones, so it can’t be used when the count should include both of them.

#include <iostream>
#include <vector>
#include <string>

using std::cout; using std::cerr;
using std::endl; using std::string;

size_t countOccurrences(char c, string &str)
{
    size_t count = 0;

    for (char i : str)
        if (i == c)
            count++;

    return count;
}

int main() {
    char ch1 = 'e';
    char ch2 = 'h';
    string str1("hello there, how are you doing?");
    string str2("Hello there! How are you doing?");

    cout << "number of char - '" << ch1 << "' occurrences = " << countOccurrences(ch1, str1) << endl;
    cout << "number of char - '" << ch2 << "' occurrences = " << countOccurrences(ch2, str2) << endl;

    exit(EXIT_SUCCESS);
}

Output:

number of char - 'e' occurrences = 4
number of char - 'h' occurrences = 1

Use the std::tolower Function to Count Character Occurrences Regardless of Letter Case

Another method to implement the previous example is to rewrite the countOccurrences function to compare the converted values of the characters each iteration. Note that std::tolower is tricky to use in that it requires the only char argument to be representable as unsigned char; otherwise, the behavior is undefined. Thus, we pass the cast unsigned char value to the tolower function and then cast it back to the char to store into a temporary variable - tmp. Additionally, we call the tolower function every iteration to convert the string characters to lower case letters for the comparison statement. Note that the following code sample can distinguish the characters in the capital and lower case and count them all.

#include <iostream>
#include <vector>
#include <string>

using std::cout; using std::cerr;
using std::endl; using std::string;

size_t countOccurrences(char c, string &str)
{
    size_t count = 0;
    char tmp = static_cast<char>(tolower(static_cast<unsigned char>(c)));

    for (char i : str)
        if (tolower(static_cast<unsigned char>(i)) == tmp)
            count++;

    return count;
}

int main() {
    char ch1 = 'e';
    char ch2 = 'h';
    string str1("hello there, how are you doing?");
    string str2("Hello there! How are you doing?");

    cout << "number of char - '" << ch1 << "' occurrences = " << countOccurrences(ch1, str1) << endl;
    cout << "number of char - '" << ch2 << "' occurrences = " << countOccurrences(ch2, str2) << endl;

    exit(EXIT_SUCCESS);
}

Output:

number of char - 'e' occurrences = 4
number of char - 'h' occurrences = 3
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Char

  • Convert ASCII to Char in C++
  • Convert Char Array to String in C++