From 5743ba3e0ea2578c562768053543859d6d8f95e5 Mon Sep 17 00:00:00 2001 From: Akshay Deshmukh Date: Wed, 14 Oct 2020 20:23:33 +0530 Subject: [PATCH] Create FreqofChar.c --- Strings/FreqofChar.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Strings/FreqofChar.c diff --git a/Strings/FreqofChar.c b/Strings/FreqofChar.c new file mode 100644 index 0000000..605343c --- /dev/null +++ b/Strings/FreqofChar.c @@ -0,0 +1,53 @@ +include +#include + +// 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); +}