# C Program to find the frequency of characters in a String devquora

Posted On: Jan 27, 2023 ### Overview:

As we know that time is the most important concern in our life. To do any task easily, We have to use software to perform it in a simpler manner. Software is a set of programs with related documents. The Software Program or Program is a set of instructions to do a specific task. Here, our specific task is to write a C Program to Find the Frequency of Characters in a String. First, I want to explain What is String? Then, I would like to explain What is the Frequency of Characters in a String? Furthermore, I would like to demonstrate how to Find the Frequency of Characters in a String. After all, I will write the logic of a C Program to Find the Frequency of Characters in a String with output.

### What is a String?

String is a combination of characters that may be any digit from 0 to 9 and Alphabets from A to Z. The repetition of characters does not matter. For instance, a String Conax uses 5 characters i.e. C,o,n,e,and x.

### What is the Frequency of Characters in a String?

The frequency of a character in a given string is how many times a particular character is present in a given string. For example, if a user enters the string as Welcome User, and wants to check for the frequency of a character say e. Then it will be 3. Because e occurs 3 times in the given string Welcome User.

### Logic to Find the Frequency of Characters in a String

To concatenate two strings str1 and str2, you will copy all characters of str2 at the end of str1. Below is the step by step descriptive logic to concatenate two strings:

• Step 1: Initialize the variables.
• Step 2: Accept the input.
• Step 3: Initialize a for loop and terminate it at the end of string.
• Step 4: This for loop will be used to count the number of time each character is present.
• Step 5: Initialize another for loop to print the frequency if it is at least 1.

### C Program to Find the Frequency of Characters in a String

```#include <stdio.h>

int main()
{
//Initializing variables.
char str;
int i;
int freq = {0};

//Accepting inputs.
printf("Enter the string: ");
gets(str);

//Calculating frequency of each character.
for(i = 0; str[i] != '\0'; i++)
{
freq[str[i]]++;
}

//Printing frequency of each character.
for(i = 0; i < 256; i++)
{
if(freq[i] != 0)
{
printf("The frequency of %c is %d\n", i, freq[i]);
}
}
return 0;
}

```

Save this program with the filename and extension .c like filename.c. Then compile it on turbo C or another compiler.

The output of the Program:

Enter the string: Welcome User

The frequency of is 1

The frequency of U is 1

The frequency of W is 1

The frequency of c is 1

The frequency of e is 3

The frequency of l is 1

The frequency of m is 1

The frequency of o is 1

The frequency of r is 1

The frequency of s is 1 ### Conclusion:

To calculate the Frequency of Characters in a String we will use a for loop that will iterate each character of the string and then store the count of each of the characters in an array. Then we will use another for loop to print the frequency of each character that we have stored in an array.

Never Miss an Articles from us.

## Related Articles

#### C Program to convert Decimal to Binary Number

A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

#### C program to reverse a string

This program reversed the entered string that means opposite of the previous string sequence...

#### C program to reverse an Integer number

Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..