C program to find the frequency of characters in a string Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a string S containing lowercase English characters, the task is to find the frequency of all the characters in the string. ExamplesInput: S="geeksforgeeks" Output: e - 4 f - 1 g - 2 k - 2 o - 1 r - 1 s - 2 Input: S="gfg" Output: f - 1 g - 2Approach 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', the 1st 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 to find the frequency // of characters in a string #include <stdio.h> #include <string.h> // Function to print the frequencies // of each character of the string void 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 string void 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 Code int main() { char S[100] = "geeksforgeeks"; findFrequncy(S); } Output:e - 4 f - 1 g - 2 k - 2 o - 1 r - 1 s - 2Complexity of the above method Time Complexity: O(N) Auxiliary Space: O(26) Comment More infoAdvertise with us A arifshaikh Follow Improve Article Tags : Strings Hash C Programs DSA frequency-counting +1 More Practice Tags : HashStrings Explore Basics & PrerequisitesLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 4 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like