Finding the frequency of characters in a string is a common problem in programming, often used in text analysis, cryptography, and data compression. This article presents three different C Program to Find the Frequency of Characters in a String, complete with explanations, example code, and outputs.
Prerequisites
Before diving into the solutions, ensure you have the following prerequisites:
- Basic understanding of C programming.
- Familiarity with arrays, strings, and loops.
- A C compiler installed on your system (e.g., GCC).
Solution 1: Using an Array
1.1 Explanation
In this approach, we use an array to store the frequency of each character. The array index corresponds to the ASCII value of the character, and the value at each index represents the frequency of the corresponding character.
1.2 Program
#include <stdio.h>
#include <string.h>
void findFrequency(char *str) {
int frequency[256] = {0}; // Array to store frequency of characters
// Calculate frequency of each character
for (int i = 0; str[i] != '\0'; i++) {
frequency[(int)str[i]]++;
}
// Print the frequency of characters
printf("Character frequencies:\n");
for (int i = 0; i < 256; i++) {
if (frequency[i] != 0) {
printf("%c: %d\n", i, frequency[i]);
}
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove newline character
findFrequency(str);
return 0;
}
1.3 Output
Enter a string: hello world
Character frequencies:
d: 1
e: 1
h: 1
l: 3
o: 2
r: 1
w: 1
Solution 2: Using a Structure
2.1 Explanation
In this approach, we define a structure to store characters and their frequencies. We use an array of this structure to keep track of the characters and their frequencies.
2.2 Program
#include <stdio.h>
#include <string.h>
typedef struct {
char character;
int frequency;
} CharFrequency;
void findFrequency(char *str) {
CharFrequency freq[256];
int count = 0;
for (int i = 0; i < strlen(str); i++) {
int found = 0;
for (int j = 0; j < count; j++) {
if (freq[j].character == str[i]) {
freq[j].frequency++;
found = 1;
break;
}
}
if (!found) {
freq[count].character = str[i];
freq[count].frequency = 1;
count++;
}
}
// Print the frequency of characters
printf("Character frequencies:\n");
for (int i = 0; i < count; i++) {
printf("%c: %d\n", freq[i].character, freq[i].frequency);
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove newline character
findFrequency(str);
return 0;
}
Output
Enter a string: hello world
Character frequencies:
h: 1
e: 1
l: 3
o: 2
w: 1
r: 1
d: 1
Solution 3: Using a Hash Table
3.1 Explanation
In this approach, we use a hash table to store the frequency of characters. This method is efficient and easy to implement using an array, where the hash function maps characters to array indices.
3.2 Program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char character;
int frequency;
} HashTable;
void findFrequency(char *str) {
HashTable *table = (HashTable *)calloc(256, sizeof(HashTable));
// Initialize hash table
for (int i = 0; i < 256; i++) {
table[i].character = (char)i;
table[i].frequency = 0;
}
// Calculate frequency of each character
for (int i = 0; str[i] != '\0'; i++) {
table[(int)str[i]].frequency++;
}
// Print the frequency of characters
printf("Character frequencies:\n");
for (int i = 0; i < 256; i++) {
if (table[i].frequency != 0) {
printf("%c: %d\n", table[i].character, table[i].frequency);
}
}
free(table);
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove newline character
findFrequency(str);
return 0;
}
Output
Enter a string: hello world
Character frequencies:
h: 1
e: 1
l: 3
o: 2
: 1
w: 1
r: 1
d: 1
Conclusion
Finding the frequency of characters in a string is a fundamental problem that can be solved using various methods in C programming. This article demonstrated three different approaches: using an array, using a structure, and using a hash table. Each method has its advantages and provides a unique way to handle the problem. By understanding these techniques, you can efficiently analyze text data and implement robust solutions in C. Experiment with these approaches to deepen your understanding and improve your programming skills.