# Count Frequency of Particular Character of the String

Count Frequency of Particular Character of the String” is a basic problem of string data structure. Here, we are given a string of length ‘n’ and our task is to count how many times a particular character occurs in the string.

Example:

```Input:
Enter a string: abcdrfedsdfddgr
Enter character : d
Output:
The number of occurrences of d in abcdrfedsdfddgr is 3```

The simplest solution of the above problem is to traverse the given string and count each occurrence of the given character in the string.

C++ Program to count frequency of particular character of the string is as follows:

```#include<bits/stdc++.h>
#define len 1000
using namespace std;
int main()
{
char str[len];
cout<<"Enter the string:\n";
cin>>str;
char ch;
cout<<"Enter a character:\n";
cin>>ch;
int count = 0;
/* Loop to traverse the string and
count the occurrences of the character
in the string */
for(int i = 0;i < len && str[i]!='\0'; i++)
{
if(str[i] == ch)
count++;
}
cout<<"\nThe number of occurrences of "<<ch<<" in "<<str<<" is "<<count;
}

```
```OUTPUT:
Enter the string: Helpmestudybro
Enter a character: e
The number of occurrences of e in Helpmestudybro is 2
Time Complexity:
O(n), where n is the length of the string.```

Related Posts: