C program to find the frequency of characters in a string - GeeksforGeeks

09 Oct, 2020
Given a string S containing lowercase English characters, the task is to find the frequency of all the characters in the string.
Examples:
Input: S=”geeksforgeeks”Output:e – 4f – 1g – 2k – 2o – 1r – 1s – 2
Input: S=”gfg”Output:f – 1g – 2
Approach: Follow the steps to solve the problem:
Initialize an array freq[] to store the frequency of each alphabet in the given string. The 0th index stores the frequency of the character ‘a’, 1st/sup> index stores the frequency of the character ‘b’ and so on.Iterate over the given string S and increment the frequency of each character encountered by 1, by performing freq[S[i] – ‘a’] += 1. If S[i] = ‘a’, then S[i] – ‘a’ is equal to 0, therefore the frequency of ‘a’ is incremented in the array.=After complete traversal of the string, print the frequency of all the characters in the string by traversing the array freq[].
Initialize an array freq[] to store the frequency of each alphabet in the given string. The 0th index stores the frequency of the character ‘a’, 1st/sup> index stores the frequency of the character ‘b’ and so on.
Iterate over the given string S and increment the frequency of each character encountered by 1, by performing freq[S[i] – ‘a’] += 1. If S[i] = ‘a’, then S[i] – ‘a’ is equal to 0, therefore the frequency of ‘a’ is incremented in the array.=
After complete traversal of the string, print the frequency of all the characters in the string by traversing the array freq[].
Below is the implementation of the above approach:
C
// C program for the above approach#include <stdio.h>#include <string.h>  // Function to print the frequencies// of each character of the stringvoid printFrequency(int freq[]){    for (int i = 0; i < 26; i++) {          // If frequency of the        // alphabet is non-zero        if (freq[i] != 0) {              // Print the character and            // its respective frequency            printf("%c - %d\n",                   i + 'a', freq[i]);        }    }}  // Function to calculate the frequencies// of each character of the stringvoid findFrequncy(char S[]){    int i = 0;      // Stores the frequencies    // of each character    int freq[26] = { 0 };      // Traverse over the string    while (S[i] != '\0') {          // Increment the count of        // each character by 1        freq[S[i] - 'a']++;          // Increment the index        i++;    }      // Function call to print    // the frequencies    printFrequency(freq);}  // Driver Codeint main(){    char S[100] = "geeksforgeeks";    findFrequncy(S);}
e - 4
f - 1
g - 2
k - 2
o - 1
r - 1
s - 2

Time Complexity: O(N)Auxiliary Space: O(26)
frequency-counting
C Programs
Hash
Strings
Hash
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C Program to read contents of Whole File
Producer Consumer Problem in C
C / C++ Program for Dijkstra's shortest path algorithm | Greedy Algo-7
Regular expressions in C
Handling multiple clients on server with multithreading using Socket Programming in C/C++
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Internal Working of HashMap in Java
Hashing | Set 1 (Introduction)
Hashing | Set 3 (Open Addressing)
Count pairs with given sum