Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program to find minimum occurrence of character in a string
In C programming, finding the minimum occurrence of a character in a string involves counting the frequency of each character and then determining which character appears the least number of times.
Syntax
char string[SIZE]; int frequency[CHARS]; // Count frequency of each character // Find character with minimum frequency
String Declaration and Initialization
Before finding minimum occurrence, let's understand string basics −
Declaration:
char stringname[size];
For example − char string[50]; creates a string of length 50 characters.
Initialization:
- Using single character constants −
char string[10] = {'H', 'e', 'l', 'l', 'o', '\0'};
- Using string constants −
char string[10] = "Hello";
Algorithm for Finding Minimum Occurrence
The logic to find minimum occurrence of a character in a given string is as follows −
- Initialize frequency array to count occurrences of each character
- Traverse the string and count frequency of each character
- Find the character with minimum non-zero frequency
for(i=0; i<CHARS; i++){
if(frequency[i]!=0){
if(frequency[minimum] == 0 || frequency[i] < frequency[minimum])
minimum = i;
}
}
Example
Following is the C program to find minimum occurring character in a string −
Note: This program uses
fgets()instead of deprecatedgets()function for safe string input.
#include <stdio.h>
#include <string.h>
#define SIZE 100 // Maximum string size
#define CHARS 256 // ASCII characters
int main() {
char string[SIZE];
int frequency[CHARS];
int i = 0, minimum;
int value;
printf("Enter the string: ");
fgets(string, SIZE, stdin);
// Remove newline character from fgets
string[strcspn(string, "
")] = '\0';
// Initialize frequency array
for(i = 0; i < CHARS; i++) {
frequency[i] = 0;
}
// Count frequency of each character
i = 0;
while(string[i] != '\0') {
value = (int)string[i];
frequency[value] += 1;
i++;
}
// Find minimum frequency character
minimum = -1;
for(i = 0; i < CHARS; i++) {
if(frequency[i] != 0) {
if(minimum == -1 || frequency[i] < frequency[minimum]) {
minimum = i;
}
}
}
if(minimum != -1) {
printf("Minimum occurrence character is '%c' = %d times.
", minimum, frequency[minimum]);
} else {
printf("No characters found in string.
");
}
return 0;
}
Output
Enter the string: tutorialspoint Minimum occurrence character is 'a' = 1 times.
How It Works
- Create a frequency array to store count of each ASCII character
- Initialize all frequencies to zero
- Traverse the input string and increment frequency for each character
- Find the character with minimum non-zero frequency
- Display the result
Key Points
- The program uses ASCII values as array indices to count character frequencies
- Only characters that actually appear in the string are considered for minimum
- Time complexity is O(n) where n is the length of the string
- Space complexity is O(1) as we use fixed size array for ASCII characters
Conclusion
Finding the minimum occurrence character in a string requires frequency counting and comparison. This approach efficiently handles all printable ASCII characters and provides the result in linear time.
