# C++ program to find the frequency of a character in a string using Count Array

This C++ program will read a string and count frequency of a character using count array.
Submitted by Abhishek Jain, on April 09, 2017

Through this program our aim is to see the implementation (how it works) of count array and its use.

In general, the solution of this problem can be achieved by a simple program as:

﻿

```#include<iostream>
#include<cstring>
using namespace std;

int main()
{
string s;
char k; int i,c=0;

cout<<"Enter String:";
cin>>s;
cout<<"Enter Character(a-z) : ";
cin>>k;

for(i=0;i<s.size();i++)     // size() return the size of the string
{
if(s[i]==k)             /*here we check for each string element,if string element s[i] is
equalto character k then increase c by 1.*/
c++;
}
cout<<k<<" occurs "<<c<<" times"<<endl;
return 0;
}
```

Output

```Enter String:IncludeHelp
Enter Character(a-z) : l
l occurs 2 times
```

But our concern is all about with “Count Array”. So first, let see

## What is Count Array?

Count Array is a Array use to count the every element of String, Array (character, Number etc) or Number (digits) by incrementing (their occurrence) at the index same as that of the element .

Count Array Always initializes with 0.
For Ex: We have a string “abhi”.
We take the count array of size 26 ( No. of alphabets = 26).
Count={0,0,0,0,0, ...}

1) At first, ‘a’ is countered from the string. Since ‘a’ is the first in alphabetical order so Count is incremented by 1.
Count{1,0,0,0,0, ...}
2) Now Second element of the String is ‘b’. It comes 2nd in alphabetical order. So Count is incremented by 1.
Count{1,1,0,0,0, ...}
3) Now ‘h’ comes at 8th in alphabetical order.so Count  is incremented by 1.
Count{1,1,0,0,0,0,0,1, ...}
...

n) All this we can do till the end of the string.

### Use of Count Array

Count Array is generally suitable for comparison between two strings (To check whether a string2 can be formed from string1), string manipulation etc… Because it reduces the time complexity from O(n2) to O(n).

Now, we come back to the program “To find the frequency of a character in a string using Count array”.

Here count array works as same as described above.

﻿

```#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string s;
char k;

cout<<"Enter String:";
cin>>s;
cout<<"Enter Character(a-z)";
cin>>k;

int i,r;
int count={0}; //initialize  count array with 0.

for(i=0;i<s.size();i++)
count[s[i]-'a']++;
/*As we all know that index of array is
numeric(integer) value .Thus,here  s[i] is
equivalent to its ASCII value, to counter this
problem we subtract with ASCII value of 'a'.*/

cout<<k<<" Occurs "<<count[k-'a']<<" times"<<endl;
return 0;
}
```

Output

```Enter String:IncludeHelp
Enter Character(a-z) : l
l occurs 2 times
```

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates