C Program to Find the Frequency of Characters in a String
Example: Find the Frequency of Characters
#include <stdio.h>
int main()
{
char str[1000], ch;
int i, frequency = 0;
printf("Enter a string: ");
gets(str);
printf("Enter a character to find the frequency: ");
scanf("%c",&ch);
for(i = 0; str[i] != '\0'; ++i)
{
if(ch == str[i])
++frequency;
}
printf("Frequency of %c = %d", ch, frequency);
return 0;
}
Output
Enter a string: This website is awesome. Enter a character to find the frequency: e Frequency of e = 4
In this program, the string entered by the user is stored in variable str.
Then, the user is asked to enter the character whose frequency is to be found. This is stored in variable ch.
Now, using the for loop, each character in the string is checked for the entered character.
If, the character is found, the frequency is increased. If not, the loop continues.
Finally, the frequency is printed.
a step-by-step explanation of a C program that finds the frequency of characters in a given string:
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // define a macro to represent the maximum size of the string
int main() {
char str[MAX_SIZE];
int freq[256] = {0}; // initialize a frequency array with all elements set to 0
int i, len;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // read a line of text from the standard input stream
len = strlen(str); // get the length of the string
for (i = 0; i < len; ++i) { // loop through each character in the string
++freq[str[i]]; // increment the frequency of the current character
}
printf("Frequency of characters in the string:\n");
for (i = 0; i < 256; ++i) { // loop through each character code (ASCII code)
if (freq[i] != 0) { // check if the frequency of the current character is not 0
printf("'%c' occurs %d times\n", i, freq[i]); // print the character and its frequency
}
}
return 0; // indicate successful completion of the program
}
Let’s break it down step-by-step:
We include the necessary header files:
stdio.h
for input/output functions andstring.h
for string handling functions.We define a macro
MAX_SIZE
to represent the maximum size of the string.We declare a character array
str
of sizeMAX_SIZE
to hold the input string, an integer arrayfreq
of size256
to store the frequency of each character, and integer variablesi
andlen
.We prompt the user to enter a string using
printf()
, and read a line of text from the standard input stream usingfgets()
, which takes in three arguments: the character array to store the input, the size of the array, and the input stream (in this case,stdin
for standard input).We get the length of the string using
strlen()
, which returns the number of characters in the string (excluding the null terminator).We use a
for
loop to iterate through each character in the string. The loop runs fromi = 0
toi < len
, wherelen
is the length of the string.Inside the loop, we use
++freq[str[i]]
to increment the frequency of the current character. This uses the ASCII code of the character as the index of thefreq
array.Once the loop is complete, we use
printf()
to print the frequency of each character in the string. We use anotherfor
loop to iterate through each character code (ASCII code), and check if the frequency of the current character is not 0. If it is not 0, we print the character and its frequency usingprintf()
.Finally, we return
0
to indicate successful completion of the program.