C program to find lowest frequency character in a string

Category: C Program
Tags: #cprogram#string

Learn how to write a C program to find the lowest frequency character in a string. This comprehensive guide includes step-by-step instructions, code implementation, and detailed explanations for beginners in C programming.

In this article, we will explore how to write a C program to find the character with the lowest frequency in a given string. This task is essential in various text processing applications, such as analyzing text for unique characters, optimizing data storage, and performing frequency analysis in natural language processing.

Objective

The goal is to create a C program that takes a string as input and identifies the character that appears the least frequently. If multiple characters have the same lowest frequency, the program will return the first one encountered.

Approach

To solve this problem, we will:

  1. Read the input string from the user.
  2. Use an array to count the frequency of each character.
  3. Identify the character with the lowest non-zero frequency.
  4. Output the lowest frequency character along with its count.

Detailed Steps

  1. Read the input string:
    • Use standard input functions to get the string from the user.
  2. Count character frequencies:
    • Initialize an array to keep track of the frequency of each character.
    • Iterate over the string and update the frequency array.
  3. Find the lowest frequency character:
    • Iterate over the frequency array to find the character with the lowest non-zero frequency.
  4. Print the result:
    • Output the character with the lowest frequency and its count.

Write a C program to find lowest frequency character in string

Below is the complete C program that implements the above steps:

#include <stdio.h>
#include <string.h>

#define ASCII_SIZE 256

int main() {
    char str[100];
    int freq[ASCII_SIZE] = {0};
    int min_freq = __INT_MAX__;
    char min_char = '\0';

    // Input the string
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character from string if present
    size_t len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';
    }

    // Count frequency of each character
    for (int i = 0; i < strlen(str); i++) {
        freq[(int)str[i]]++;
    }

    // Find the character with the lowest frequency
    for (int i = 0; i < ASCII_SIZE; i++) {
        if (freq[i] > 0 && freq[i] < min_freq) {
            min_freq = freq[i];
            min_char = (char)i;
        }
    }

    // Print the lowest frequency character and its count
    if (min_char != '\0') {
        printf("The lowest frequency character is '%c' with a frequency of %d.", min_char, min_freq);
    } else {
        printf("No characters found in the string.");
    }

    return 0;
}

Output

Enter a string: learn programming at ProCoding
The lowest frequency character is 'C' with a frequency of 1.

Explanation of the Program

  1. Input Handling:
    • The program uses fgets() to read a line of text from the user, ensuring that spaces and special characters are included.
    • It removes any trailing newline character added by fgets().
  2. Frequency Count:
    • An array freq of size 256 (to cover all ASCII characters) is initialized to zero.
    • The program iterates over each character in the string, incrementing the corresponding index in the freq array based on the ASCII value of the character.
    • int freq[ASCII_SIZE] = {0}; this is a shorthand way to initialize every element in the array to 0.
  3. Identify Lowest Frequency Character:
    • The program iterates over the freq array to find the minimum non-zero frequency and the corresponding character. It initializes min_freq to the maximum integer value (__INT_MAX__) to ensure any frequency found will be smaller.
  4. Output:
    • The character with the lowest frequency and its count are printed. If no characters are found (in case of an empty string), the program outputs a relevant message.

Recommended Posts