Count the Number of Occurrences of a Character in a String in C++

In this exercise, you will write a C++ program that counts how many times a specific character appears in a given string. The program will take both a string and a character as input from the user. It will then loop through the string and use a counter to track the number of occurrences of the specified character.

This exercise is useful for understanding how to work with strings and loops in C++. It also demonstrates how to use the `char` and `string` data types together.

Group

Flow Control in C++

Objective

1. Prompt the user to input a string.
2. Ask the user for a character to search for.
3. Use a loop to iterate through the string and count how many times the character appears.
4. Display the result to the user.

Write a program that counts how many times a character appears in a string.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include the iostream library for input and output
#include <string> // Include the string library to work with string data types

using namespace std; // Use the standard namespace

// Main function - entry point of the program
int main() {
    string str; // Variable to store the input string
    char ch; // Variable to store the character to search for
    int count = 0; // Variable to count the occurrences of the character

    // Prompt the user to enter a string
    cout << "Enter a string: ";
    getline(cin, str); // Read the entire line of input into the string

    // Ask the user to input the character to search for
    cout << "Enter a character to search for: ";
    cin >> ch; // Read the character input

    // Loop through the string to count occurrences of the character
    for (int i = 0; i < str.length(); i++) { // Iterate through each character in the string
        if (str[i] == ch) { // If the current character matches the one being searched
            count++; // Increment the count
        }
    }

    // Output the result to the user
    cout << "The character '" << ch << "' appears " << count << " times in the string." << endl;

    return 0; // Return 0 to indicate successful execution of the program
}

 Output

Enter a string: Hello World
Enter a character to search for: o
The character 'o' appears 2 times in the string.

//Or for another string:
Enter a string: OpenAI is great
Enter a character to search for: i
The character 'i' appears 2 times in the string.

Share this C++ Exercise


More C++ Programming Exercises of Flow Control in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Print a Pattern of Numbers in C++

    In this exercise, you will create a C++ program that prints a pattern of numbers from 1 to a specified number. The program will prompt the user to input the number up to which they...

  • Determine if a Grade is Passing or Failing in C++

    This exercise is designed to help you understand conditional statements in C++. You will create a simple program that takes a numeric grade input from the user and evaluates whethe...

  • Calculate the Factorial of a Number Using a Loop in C++

    This exercise teaches you how to calculate the factorial of a number using an iterative approach in C++. The factorial of a number N is the product of all positive integers less th...

  • Print Numbers from 1 to 100 Using a For Loop in C++

    In this exercise, you will learn how to use a `for` loop in C++ to print numbers from 1 to 100. A `for` loop is commonly used when you know the number of iterations in advance. In ...

  • Print a Triangle of Asterisks in C++

    In this exercise, you will write a C++ program that prints a triangle pattern using asterisks (`*`). This is a common exercise used to practice loops and nested loops. The pattern ...

  • Print Numbers from 10 to 1 in Reverse Order in C++

    In this exercise, you will write a C++ program that prints numbers from 10 to 1 in reverse order. This will help you practice using loops, particularly with decrementing values. By...