Introduction
You'll learn how to find the frequency of a character in a string and obtain the code to do it.
How many times does a specific character appear in a string in that instance?
For example, if the user types in coding ninja and wants to verify the frequency of a particular character, such as n. Then it'll be three because the letter n appears three times in the provided string, coding ninja.
Problem Statement
Write a C program to find the frequency of characters in a given string.
Approach
- The string entered by the user is saved in str in this program.
- The user is then asked to enter the character whose frequency will be determined. Variable ch is used to hold this information.
- The string is then iterated over using a loop. The count is incremented by one in each iteration if the character in the string is equal to the ch. Finally, the count variable's frequency is printed.
Implementation
You can also try this code with Online C Compiler |
Time Complexity: O(n), where n is the length of a string.
Space Complexity: Constant space.
Output
Program Explanation
- The gets() function is used to get string input from the user.
- The scanf() function is used to convey character input from the user.
- We've established a for loop to verify every character in the string, starting with the 0th character and ending with the null-terminated character' \0'.
- When the character matches the character at any point in the string, the count variable's value is incremented. Because the count variable is initially set to 0, the count represents the total number of times the character appears in the string.
For example, suppose the string is codingninja (in str variable) and the character is n (in ch variable). Therefore, str[0] holds c, str[1] holds o, str[2] holds d, str[3] holds i, ....., str[10] holds j, and str[11] holds a.
The character n presents the index numbers 4, 6, and 8. Total of 3 times. Therefore, the count variable gets incremented three times. As the condition ch==str[i] gets true three times. Because ch holds c and str[4], str[6], and str[8] also holds n. Therefore, the frequency of n in the given string say codingninja will be 3.
Alternate Method
In this method, we are finding the frequency of each character in the string irrespective of user’s input at first. Let’s see the approach for the same:
Approach
- To store the frequency of each alphabet in the given string, create an array freq[]. The frequency of the character 'a' is stored in the 0th index, The frequency of the character 'b' is stored in the 1th index, and so on.
- Iterate through the string S, increasing the frequency of each character encountered by 1 for each iteration.
- Then after the complete iteration, print the frequency of all the characters in the string.
Algorithm
- The string entered by the user is saved in S in this program.
- Store the frequency of each alphabet in the given string
- To store the frequency we simple use freq[S[i] - 'a']++;
- Finally, print the frequency of each character in the given string.
You can also try this code with Online C Compiler |
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(n), where n is the length of the string.
Output
You can practice by yourself with the help of online c compiler.