C program to find minimum occurring character in a string

Write a C program to find minimum occurring character in a string using loop. How to find lowest frequency character in a string using loop in C programming. Program to find minimum frequency character in a given string in C. Logic to find least occurring character in a given string.

Example

Input

Input string: I love learning programming at Codeforwin.

Output

Minimum occurring character is '.' = 1

Required knowledge

Basic C programming, Loop, Array, String

Must know -

Logic to find minimum occurring character in string

Below is the step by step descriptive logic to find minimum occurring character in a given string.

  1. Input string from user, store it in some variable say str.
  2. Declare another array to store frequency of all alphabets, say freq[26]. I have declared size of freq as 26 since there are 26 alphabets in English.
  3. Initialize frequencies of all alphabets in freq array to 0.
  4. Find frequency of each character present in the string.
  5. Minimum occurring character in string is minimum occurring value in the freq array.

Program to find lowest frequency character in string

/**
 * C program to find minimum occurring character in a string
 */

#include <stdio.h>
#define MAX_SIZE 100  // Maximum string size
#define MAX_CHARS 255 // Maximum characters allowed


int main()
{
    char str[MAX_SIZE];
    int freq[MAX_CHARS]; //Stores frequency of each character
    int i = 0, min;
    int ascii;

    printf("Enter any string: ");
    gets(str);

    /* Initialize frequency of all characters to 0 */
    for(i=0; i<MAX_CHARS; i++)
    {
        freq[i] = 0;
    }


    /* Finds frequency of each characters */
    i=0;
    while(str[i] != '\0')
    {
        ascii = (int)str[i];
        freq[ascii] += 1;

        i++;
    }


    /* Finds minimum frequency */
    min = 0;
    for(i=0; i<MAX_CHARS; i++)
    {
        if(freq[i] != 0)
        {
            if(freq[min] == 0 || freq[i] < freq[min])
                min = i;
        }
    }


    printf("Minimum occurring character is '%c' = %d.", min, freq[min]);

    return 0;
}

Read more - Program to find maximum occurring character in a string

Output

Enter any string: I love learning programming at Codeforwin.
Minimum occurring character is '.' = 1.

Happy coding 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols. as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Follow on: Facebook | Twitter | Google | or

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>