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 [Last updated : February 27, 2023]

Finding the frequency of a character in a string using Count Array

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[26]={0,0,0,0,0, ...}

1) At first, 'a' is countered from the string. Since 'a' is the first in alphabetical order so Count[0] 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[1] is incremented by 1.
Count{1,1,0,0,0, ...}
3) Now 'h' comes at 8th in alphabetical order.so Count [7] 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.

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

#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[26] = { 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


Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.