Find Highest Frequency Character in a String in C Language

Spread the love
find-highest-frequency-character-in-a-string-in-c-language

Program Description:

Write a Program to Find highest frequency character in a string in C programming language. The program will accept a string from the user and prints the most frequent character in the given string along with the character frequency.

We are going to look at a few methods to find the highest frequency character or most frequent character in a string. Each program will be followed by a detailed step-by-step explanation and program output. We also provided instructions for compiling and running the program using your compiler.

📢 This Program is part of the C String Practice Programs series

Let’s look at the program input and output.

Example Input and Output:

Input:

Enter a string : Fuzzy Wuzzy

Output:

Frequency of Characters in given string :

Character : ' ' - Frequency: 1

Character : 'F' - Frequency: 1

Character : 'W' - Frequency: 1

Character : 'u' - Frequency: 2

Character : 'y' - Frequency: 2

Character : 'z' - Frequency: 4

Character 'z' is the High Frequency Character(Frequency: 4) in the given string

In the above example, The character z is the most frequent character in the word Fuzzy Wuzzy.

Program Prerequisites:

It is recommended to know the basics of the string, and arrays in the c programming language. Please go through the following articles to refresh your concepts.

We will look at the two different methods to find the highest frequency character in a string in C Language.

  • Iterative Method
  • Using User-defined Functions

Algorithm to Find Highest Frequency Character in a String in C:

Step 1: Create two macros to store the string SIZE and ASCIISIZE

Step 2: Declare a string to store the input with size of SIZEchar inputStr[SIZE];

Step 3: Create another string to store the frequencies and initialize it with zeroes. – char frequency[ASCIISIZE] = {0};

Step 4: Prompt the user for the input string and read it using the gets function and update the inputStr string.

Step 5: Iterate over the inputStr and update the frequency array with the frequency of each character in the string. Please look at the Count Frequency of characters in the string program for a detailed explanation.

Step 6: Use another loop to iterate over the frequency array and find the highest frequency character by updating the highFreq variable – if(highFreq < frequency[i]) update highFreq = frequency[i]; and ch = i ( Note: We can use above step 5 loop to get the highFreq and ch but for brevity we used two loops)

Step 7: Once the above loop is completed, The highFreq variable will contain the highest frequency and ch character will contain the most frequent character in the string.

Step 8: Print the results on the console.

Program to Find highest frequency character in a string in C – Iterative Method:

Let’s covert the above algorithm into a working C program.

Program Output:

Compile the program using your favorite compiler. We are using the GCC compiler here.

The above command generates an executable file named a.out. Run the executable file.

Test 1:

Program-to-Find-highest-frequency-character-in-a-string-in-C-Iterative-Method

Let’s try a few more examples.

Test 2:

Test 3:

As we can see from the above program, The program is properly calculating the most frequent character in the string with it’s frequency.

Find Highest Frequency Character in a String in C using a user-defined function:

In the above program, We have written all the code inside the main() function. Which is not a recommended method. So lets rewrite the above program to use a user-defined function to find the highest frequency character in the string.

📢 Advantages of the Functions

We will define a user-defined function called getHighFreqChar to find the highest frequency character in the input string.

Here is the prototype of the getHighFreqChar function

char getHighFreqChar(char * inputStr)

This function takes the input string( inputStr) as the formal argument. It computes the most frequent character in the string( inputStr) and returns that character to the caller.

We can even combine Two for loops and check the highFreq number within the first for loop itself.

Program Output:

Let’s compile and Run the program.

Highest-Frequency-Character-using-function-program-output

We are getting the desired results. Program is able to find the most repeated character in the string.

Related String Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to Find Highest Frequency character in a String […]

  2. […] C Program to Find Highest Frequency character in a String […]

Leave a Reply