Program to Find Lowest Frequency character in a string in C

Spread the love
C-Program-to-Find-Lowest-Frequency-character-in-a-string

Program Description:

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

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

We are going to look at a few methods to find the lowest frequency character or least 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.

Example Input and Output:

Input:

Enter a string : ABBCCDDD

Output:

Frequency of Characters in given string :

Character : 'A' - Frequency: 1

Character : 'B' - Frequency: 2

Character : 'C' - Frequency: 2

Character : 'D' - Frequency: 3

Character 'A' is the Low Frequency Character(Frequency: 1) in the given string

In the input string ABBCCDDD, The character A is the least frequent character or rarest character.

Prerequisites:

It is recommended to know the C-Strings, C-Arrays, and Functions in C to better understand the following program.

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

  • Iterative Method
  • Using User-defined Functions

Find the Lowest Frequency character in a string in C Program Explanation:

  1. Create Two macros to store the string SIZE and ASCIISIZE with values 100 and 256 respectively.
  2. Declare a string to store the input with the size of SIZEchar inputStr[SIZE];
  3. Create another string to store the frequencies and initialize it with zeroes. – char frequency[ASCIISIZE] = {0};
  4. Prompt the user for the input string and read it using the gets function and update the inputStr string.
  5. To Calculate the Lowest Frequency Character, 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.
  6. Use another loop to iterate over the frequency array and find the Lowest frequency character by updating the lowFreq variable – if(lowFreq > frequency[i]) is true update lowFreq = frequency[i]; and rarech = i
  7. Once the above two loops are completed, The lowFreq variable will contain the least frequency and rarech character will contain the least frequent character or the rarest character in the string.
  8. Print the results on the standard Output.

Program to Find Lowest Frequency character in a string in C:

Here is the program to find the rarest character in the string in the c programming language using an iterative method.

We are also printing the frequencies of all characters to better understand the program.

Program Output:

Let’s compile and run the program. We are using the GCC compile in Ubuntu OS. but you can use any C language compiler.

Lowest-Frequency-Character-in-a-string-in-c-program-output

From the above program output, We can confirm that the program is properly calculating the least frequent character or the rarest character in a string.

Let’s try another example.

If multiple characters have the same frequency, Then the character with the lowest ASCII value will be returned.

In the above www.SillyCodes.com example, The character C (capital C)’s ASCII value is the Smallest one Compared to other characters with the frequency of One(1).

Find Lowest Frequency character in a string using user-defined functions in C:

In this method, We will move the logic to find the least frequent character in a string to a new user-defined function. This function will take a string as the input and returns the least frequent character.

Let’s modify the above program to use a custom function named getLowFreqChar to find the lowest frequency character in the string.

Here is the prototype of the getLowFreqChar function

char getLowFreqChar(char * str)

This function takes a input string( str) as a formal argument and computes the least frequency character in string( str) and returns it to the caller.

In the main() function, Take the input string from the user and update it in the str string. Then to find the rarest character in the string, Call the getLowFreqChar() function with the input string ( str).

rarech = getLowFreqChar(str);

The getLowFreqChar() function will calculate the least frequent character in the str and returns it. We are storing the return value in the rarech variable.

Print the rarech character on the console.

Program Output:

Compile and Run the Program

Lowest-Frequency-Character-in-a-string-using-functions

Here are a few examples of the program.

As we can see from the above input strings and program output, The program is generating the desired results and finding the rarest character in the string properly.

Related 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...

1 Response

  1. […] C Program to Find Lowest Frequency Character in a String […]

Leave a Reply