C program to find the lowest frequency character in a string – In this article, we will brief in on the several ways to find the highest frequency character in a string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this distinct piece are as follows:
- Using Standard Method
- Using Function
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As you can see in the image uploaded above, firstly, you need to enter the string of your concern.
Let’s take the first example itself.
The string entered is abcdabc
As we can see, the least frequency is of the character ‘d’: Only once.
Hence, multiple ways to do the same in C programming are as follows:
Using Standard Method
- Initialize count=0, declare the character array s[1000] and int array a[1000].
2) The for loop iterates through the string until the last element of the string becomes null. The value of j at the last iteration of for loop indicates the length of the string.
3) Initialize the j value to the variable k and variable n.
4) The outer for loop iterates through the string from i=0 to i<length of the string.
a) Initialize a[i]=length of the string and count=1.
b) The inner for loop compares s[i] with remaining elements of the string. If s[i] matches then increase the count value and initialize the element with null where it appears repeatedly. store the count value at a[i].
c) Store the count value at a[i].
If count value <k then initialize k=count.
5) The for loop iterates with the structure for(j=0;j<;j++) through the integer array which contains count values of the characters of the string.
If the count value a[j] equal to the minimum count value k then print the element s[j] and the k value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; int a[1000],i,j,k,count=0,n; printf("Enter the string : "); gets(s); for(j=0;s[j];j++); k=n=j; for(i=0;i<n;i++) { a[i]=n; count=1; if(s[i]) { for(j=i+1;j<n;j++) { if(s[i]==s[j]) { count++; s[j]='\0'; } } a[i]=count; if(count<=k) k=count; } } printf("minimum occuring characters "); for(j=0;j<n;j++) { if(a[j]==k) { printf(" '%c',",s[j]); } } printf("\b = %d times \n ",k); return 0; } |
Output:
1 2 3 4 5 6 7 8 |
Enter the string: abcdabc minimum occurring characters'=1 time Enter the string : hello welcome to cbeginners minimum occuring characters 'h', 'w', 'm', 't', 'b', 'g', 'i', 'r', 's'=1 times Enter the string : abcd abcd minimum occuring characters 'a', 'b', 'c', 'd', ' '=2 times |
Using Function
- The function stringlength(char *s) returns the string length.
2) The main() function calls the printmin(char *s) by passing the string as an argument to the function.
3)The function printmin(char *s) prints the minimum frequency elements.
a) The outer for loop iterates through the string until the last character of the string becomes to null.
b) The inner for loop finds the frequency of each character of the string and place the frequency numbers into the integer array a[].
c) If the frequency of any character is equal to minimum frequency k then print that element and print the k value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#include <stdio.h> #include <string.h> int stringlength(char *s) { int j; for(j=0;s[j];j++); return j; } void printmin(char *s) { int a[1000],i,j,k,count=0,n; k=n=stringlength(s); for(i=0;i<n;i++) { a[i]=n; count=1; if(s[i]) { for(j=i+1;j<n;j++) { if(s[i]==s[j]) { count++; s[j]='\0'; } } a[i]=count; if(count<=k) k=count; } } printf("minimum occuring characters "); for(j=0;j<n;j++) { if(a[j]==k) { printf(" '%c',",s[j]); } } printf("\b=%d times \n ",k); } int main() { char s[1000]; printf("Enter the string : "); gets(s); printmin(s); return 0; } |
1 2 3 4 5 6 7 8 |
Enter the string: abcd abc ab a minimum occurring characters'=1 times Enter the string : hello welcome to cbeginners minimum occuring characters 'h', 'w', 'm', 't', 'b', 'g', 'i', 'r', 's'=1 time Enter the string : never give up minimum occuring characters 'n', 'r', 'g', 'i', 'u', 'p'=1 times |