C program to find the frequency of characters in a string (original) (raw)
Last Updated : 01 Jun, 2023
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 - 4 f - 1 g - 2 k - 2 o - 1 r - 1 s - 2
Input: S="gfg" Output: f - 1 g - 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', 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 - 2
Complexity of the above method
Time Complexity: O(N)
Auxiliary Space: O(26)
Similar Reads
- C Program to Find the Length of a String The length of a string is the number of characters in it without including the null character (‘\0’). In this article, we will learn how to find the length of a string in C.The easiest way to find the string length is by using strlen() function from the C strings library. Let's take a look at an exa 2 min read
- C Program to Print the Length of a String using Pointers C language provides a built-in function strlen() but in this article, we will learn how to find and print the length of the string using pointers in C.The easiest method to find the length of a string using pointers is by calculating difference between the pointers to the first and last character of 2 min read
- Find the Length of Character Array in C In C, a character array is a collection of elements of the ‘char’ data type that are placed in contiguous memory locations and are often used to store strings. The length of a character array is defined as the total number of characters present in the array, excluding the null character ('\0') at th 2 min read
- C Program to Print the Length of a String Using %n Format Specifier In C, strings are arrays of characters terminated by a null character ('\0') and the length of a string is the number of characters before this null character. In this article, we will learn how to find the length of the string using %n format specifier.The %n is a special format specifier for print 1 min read
- How to Create a Dynamic Array of Strings in C? In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra 3 min read
- C Program to Print the First Letter of Each Word In a string that contains multiple words, each word is separated by a whitespace. In this article, we will learn how to print the first letter of each word using a C program.The simplest method to print the first letter of each word is by using a loop. Let’s take a look at an example:C#include <s 3 min read
- Length of a String Without Using strlen() Function in C The length of a string is the number of characters in it without including the null character. C language provides the strlen() function to find the lenght of the string but in this article, we will learn how to find length of a string without using the strlen() function.The most straightforward met 2 min read
- How to Find Length of a String Without string.h and Loop in C? C language provides the library for common string operations including string length. Otherwise, a loop can be used to count string length. But is there other ways to find the string length in C apart from above two methods. In this article, we will learn how to find length of a string without strin 2 min read
- C Program for Counting Sort Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Algorithm: Step 1: StartStep 2 : Find Larg 3 min read
- Check if all occurrences of a character appear together Given a string s and a character c, find if all occurrences of c appear together in s or not. If the character c does not appear in the string at all, the answer is true. Examples Input: s = "1110000323", c = '1' Output: Yes All occurrences of '1' appear together in "1110000323" Input: s = "3231131" 7 min read