- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a C Program to count the frequency of each character
Follow the algorithm to write a C program which enables to count the frequency of each character.
Algorithm
Step 1: Define MAX size. Step 2: Declare char and integer variables. Step 3: Read the string from console. Step 4: Find length of the string. Step 5: Initialize frequency of each character to 0. Step 6: Find total number of occurrences of each character. for(i=0; i<length; i++) i. if(string[i]>='a' && string[i]<='z') frequency[string[i] - 97]++; ii. else if(string[i]>='A' && string[i]<='Z') frequency[string[i] - 65]++; Step 7: Print the frequency of all characters in the string. if(frequency[i] != 0) printf("'%c' = %d
", (i + 97), frequency[i]);
Example
Given below is the C program to count frequency of each character in a string −
#include <stdio.h> #include <string.h> #define MAX 100 // Maximum string size int main(){ char string[MAX]; int i, length; int frequency[20]; /* Input string from user */ printf("enter the string:
"); gets(string); length = strlen(string); /* Initialize frequency of each character to 0 */ for(i=0; i<20; i++){ frequency[i] = 0; } /* Find total number of occurrences of each character */ for(i=0; i<length; i++){ /* If the current character is lowercase alphabet */ if(string[i]>='a' && string[i]<='z'){ frequency[string[i] - 97]++; } else if(string[i]>='A' && string[i]<='Z'){ frequency[string[i] - 65]++; } } /* Print the frequency of all characters in the string */ printf("
Frequency of all characters in string:
"); for(i=0; i<20; i++){ /* If current character exists in given string */ if(frequency[i] != 0){ printf("'%c' = %d
", (i + 97), frequency[i]); } } return 0; }
Output
When the above program is executed, it produces the following result −
enter the string: Tutorials Point Frequency of all characters in string: 'a' = 1 'i' = 2 'l' = 1 'n' = 1 'o' = 2 'p' = 1 'r' = 1 's' = 1 't' = 3
- Related Articles
- C# program to count the occurrences of each character
- Java program to count the occurrences of each character
- C++ Program to Find the Frequency of a Character in a String
- Java program to count the occurrence of each character in a string using Hashmap
- Write a Golang program to find the frequency of each element in an array
- Java Program to Find the Frequency of Character in a String
- C program to find frequency of each digit in a string
- Frequency of each character in String in Python
- Java program to find the Frequency of a character in a given String
- Print number of words, vowels and frequency of each character
- Python program to count distinct words and count frequency of them
- How to Count Occurrences of Each Character in String in Android?
- C Program to read and write character string and sentence
- Java Program to Iterate through each character of the string.
- Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary

Advertisements