Count Characters in String

Count Specific Characters In String

Write a program to count the occurrences of a specific character in a given string.

First ask the user to input a piece of text and store it in a std::string. Secondly ask the user to input a char to count occurences of.

Extra

Make use of std::tolower to make sure both upper case and lower case versions are counted.

std::tolower takes a char and returns the lowercase version of it, if it is not already lowercase.

It is found inside the <cctype> header.

Hint

You have to include <iostream> and <string>.

Use getline to avoid running into problems getting inputs with spaces.

You can use the range-based loop to cycle through each char in the std::string to count it.

Answer

Case sensitive version

#include <iostream>
#include <string>

int main() {
    // Prompt user to enter a string
    std::cout << "Enter a string: ";
    std::string inputString;
    std::getline(std::cin, inputString);

    // Ask user for the character to count
    std::cout << "Enter the character to count: ";
    char charToCount;
    std::cin >> charToCount;

    // Count occurrences of the character
    int count = 0;
    for (char ch : inputString) {
        if (ch == charToCount)
            ++count;
    }

    // Output the count
    std::cout << "The character '" << charToCount << "' appears " << count << " times in the string." << std::endl;
}

This program asks for the string input and the character to count before utilising a range-based loop to access each character of the string. Every character is compared to the character we're counting. If they match, the count is incremented. At the end the character count is output to the user through the console.

Extra

#include <iostream>
#include <string>
#include <cctype>  // To access the tolower function

int main() {

    // Prompt user to enter a string
    std::cout << "Enter a string: ";
    std::string inputString;
    std::getline(std::cin, inputString);

    // Ask user for the character to count
    std::cout << "Enter the character to count: ";
    char charToCount;
    std::cin >> charToCount;
    charToCount = std::tolower(charToCount);  // Make sure that the character we compare we count is lowercase

    // Count occurrences of the character
    int count = 0;
    for (char ch : inputString) {
	// Use std::tolower to compare the lowercase version of the character to the character we are counting (which is already lowercase)
        if (std::tolower(ch) == charToCount)
            ++count;
    }

    // Output the count
    std::cout << "The character '" << charToCount << "' appears " << count << " times in the string." << std::endl;
}

In addition to the steps from the first answer, this makes sure the comparison is made between the lowercase versions of the character we're counting.

We assign charToCount to its lowercase version before the loop to avoid optimise the program. We could use std::tolower(ch) == std::tolower(charToCount), but this would make the program have to process an extra std::tolower call for every character.

Questions