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.
Table of contents:
- What is a String?
- What is the Frequency of Characters in a String?
- Logic to Find the Frequency of Characters in a String
- C Program to Find the Frequency of Characters in a String
- Conclusion
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[100]; int i; int freq[256] = {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.