CPP Program find the frequency of characters in a string

 Here, you will learn about how to find the frequency of the character in the string in the cpp programming language.

Code:


#include <iostream>

using namespace std;


int main()

{

    string str = "CPP is best for gaming";

    char checkChar = 'i';

    int count = 0;


    for (int i = 0; i < str.size(); i++)

    {

        if (str[i] ==  checkChar)

        {

            ++ count;

        }

    }


    cout << "Number of  " << checkChar << " : " << count;


    return 0;

}


 In the above program, find the length of the string then apply the for loop with check if (str[i] ==  checkChar) increment in the count in each iteration. The loop is iterate until condition the end of the string.


If you have any problem in the above program write your query in the below comment section

Comments

Hire a programmer

Popular posts from this blog

CSS selector

C program to convert decimal to binary number

C Program to convert binary to decimal number