Q:

Program to find maximum and minimum occurring character in a string

belongs to collection: String Programs

0

Explanation

In this program, we need to count each character present in the string and find out the maximum and minimum occurring character.

Grass is greener on the other side  

In above example, character 'a' is occurred only once in the string. So, it is minimum occurring character and is highlighted by red. Character e has occurred maximum number of times in the entire string i.e. 6 times. Hence, it is the maximum occurring character and is highlighted by green.

Algorithm

  1. Define a string.
  2. Declare an array freq with the same size as that of string. Array freq will be used to store counts of unique character based upon their index.
  3. Variable minChar represent the minimum occurring character and maxChar represent the maximum occurring character. Initially, they are initialized to first character present in the string.
  4. Two loops will be used. Outer will select a character and initialize corresponding index in freq with 1.
  5. Inner loop will compare the selected character with rest of characters present in the string. If a match found, then increment the freq[i] by 1.
  6. Variable min and max is used to store the count of minimum and maximum occurring character which will initially point to count of first element of freq i.e. freq[0].
  7. Loop through the array freq and compare count of each character stored in freq with min and max.
  8. If count stored in freq is less than value of min, then store that count in min and corresponding character in minChar.
  9. If count stored in freq is more than value of max, then store that count in max and corresponding character in maxChar.
  10. At the end of the loop, minChar will store the minimum occurring character and maxChar will store the maximum occurring character.

Input:

string = "grass is greener on the other side"  

Output:

Minimum occurring character: a
Maximum occurring character: e

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

string = "grass is greener on the other side";  
freq = [None] * len(string);  
minChar = string[0];  
maxChar = string[0];  
   
#Count each word in given string and store in array freq  
for i in range(0, len(string)):  
    freq[i] = 1;  
    for j in range(i+1, len(string)):  
        if(string[i] == string[j] and string[i] != ' ' and string[i] != '0'):  
            freq[i] = freq[i] + 1;  
              
            #Set string[j] to 0 to avoid printing visited character  
            string = string[ : j] + '0' + string[j+1: ];  
              
#Determine minimum and maximum occurring characters  
min = max = freq[0];  
for i in range(0, len(freq)):  
      
    #If min is greater than frequency of a character  
    #then, store frequency in min and corresponding character in minChar  
    if(min > freq[i] and freq[i] != '0'):  
        min = freq[i];  
        minChar = string[i];  
    #If max is less than frequency of a character   
    #then, store frequency in max and corresponding character in maxChar  
    if(max < freq[i]):  
        max = freq[i];  
        maxChar = string[i];  
   
print("Minimum occurring character: " + minChar);  
print("Maximum occurring character: " + maxChar);  

Output:

Minimum occurring character: a
Maximum occurring character: e

 

C

#include <stdio.h>  
#include <string.h>  
   
int main()  
{  
    char string[] = "grass is greener on the other side";  
    int i, j, min, max, length = strlen(string);  
    char minChar = string[0], maxChar = string[0];  
    int freq[length];  
      
    //Count each word in given string and store in array freq  
    for(i = 0; i < length; i++) {  
        freq[i] = 1;  
        for(j = i+1; j < length; j++) {  
            if(string[i] == string[j] && string[i] != ' ' && string[i] != '0') {  
                freq[i]++;  
                  
                //Set string[j] to 0 to avoid printing visited character  
                string[j] = '0';  
            }  
        }  
    }  
      
    //Determine minimum and maximum occurring characters  
    min = max = freq[0];  
    for(i = 0; i < length; i++) {  
          
        //If min is greater than frequency of a character   
        //then, store frequency in min and corresponding character in minChar  
        if(min > freq[i] && freq[i] != '0') {  
            min = freq[i];  
            minChar = string[i];  
        }  
        //If max is less than frequency of a character   
        //then, store frequency in max and corresponding character in maxChar  
        if(max < freq[i]) {  
            max = freq[i];  
            maxChar = string[i];  
        }  
    }  
      
    printf("Minimum occurring character: %c\n", minChar);  
    printf("Maximum occurring character: %c", maxChar);  
          
    return 0;  
}  

 

Output:

Minimum occurring character: a
Maximum occurring character: e

 

JAVA

public class Characters   
{  
     public static void main(String[] args) {  
        String str = "grass is greener on the other side";  
        int[] freq = new int[str.length()];  
        char minChar = str.charAt(0), maxChar = str.charAt(0);  
        int i, j, min, max;          
          
        //Converts given string into character array  
        char string[] = str.toCharArray();  
          
        //Count each word in given string and store in array freq  
        for(i = 0; i < string.length; i++) {  
            freq[i] = 1;  
            for(j = i+1; j < string.length; j++) {  
                if(string[i] == string[j] && string[i] != ' ' && string[i] != '0') {  
                    freq[i]++;  
                      
                    //Set string[j] to 0 to avoid printing visited character  
                    string[j] = '0';  
                }  
            }  
        }  
          
        //Determine minimum and maximum occurring characters  
        min = max = freq[0];  
        for(i = 0; i <freq.length; i++) {  
              
            //If min is greater than frequency of a character   
            //then, store frequency in min and corresponding character in minChar  
            if(min > freq[i] && freq[i] != '0') {  
                min = freq[i];  
                minChar = string[i];  
            }  
            //If max is less than frequency of a character   
            //then, store frequency in max and corresponding character in maxChar  
            if(max < freq[i]) {  
                max = freq[i];  
                maxChar = string[i];  
            }  
        }  
          
        System.out.println("Minimum occurring character: " + minChar);  
        System.out.println("Maximum occurring character: " + maxChar);  
    }  
}  

 

Output:

Minimum occurring character: a
Maximum occurring character: e

 

C#

using System;  
                      
public class Characters  
{      
    public static void Main()  
    {  
        String str = "grass is greener on the other side";  
        int[] freq = new int[str.Length];  
        char minChar = str[0], maxChar = str[0];  
        int i, j, min, max;          
          
        //Converts given string into character array  
        char[] string1 = str.ToCharArray();  
          
        //Count each word in given string and store in array freq  
        for(i = 0; i < string1.Length; i++) {  
            freq[i] = 1;  
            for(j = i+1; j < string1.Length; j++) {  
                if(string1[i] == string1[j] && string1[i] != ' ' && string1[i] != '0') {  
                    freq[i]++;  
                      
                    //Set string1[j] to 0 to avoid printing visited character  
                    string1[j] = '0';  
                }  
            }  
        }  
          
        //Determine minimum and maximum occurring characters  
        min = max = freq[0];  
        for(i = 0; i < freq.Length; i++) {  
              
            //If min is greater than frequency of a character   
            //then, store frequency in min and corresponding character in minChar  
            if(min > freq[i] && freq[i] != '0') {  
                min = freq[i];  
                minChar = string1[i];  
            }  
            //If max is less than frequency of a character   
            //then, store frequency in max and corresponding character in maxChar  
            if(max < freq[i]) {  
                max = freq[i];  
                maxChar = string1[i];  
            }  
        }  
          
        Console.WriteLine("Minimum occurring character: " + minChar);  
        Console.WriteLine("Maximum occurring character: " + maxChar);  
    }  
}  

 

Output:

Minimum occurring character: a
Maximum occurring character: e

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
$string = "grass is greener on the other side";  
$freq = array();  
$minChar = $maxChar = $string[0];          
   
//Count each word in given string and store in array freq  
for($i = 0; $i < strlen($string); $i++) {  
    array_push($freq, 1);  
    for($j = $i+1; $j < strlen($string); $j++) {  
        if($string[$i] == $string[$j] && $string[$i] != ' ' && $string[$i] != '0') {  
            $freq[$i]++;  
              
            //Set $string[$j] to 0 to avoid printing visited character  
            $string[$j] = '0';  
        }  
    }  
}  
   
//Determine minimum and maximum occurring characters  
$min = $max = $freq[0];  
for($i = 0; $i < count($freq); $i++) {  
      
    //If min is greater than frequency of a character   
    //then, store frequency in min and corresponding character in minChar  
if($min > $freq[$i] && $freq[$i] != '0') {  
    $min = $freq[$i];  
    $minChar = $string[$i];  
}  
//If max is less than frequency of a character   
//then, store frequency in max and corresponding character in maxChar  
if($max < $freq[$i]) {  
    $max = $freq[$i];  
    $maxChar = $string[$i];  
}  
}  
   
print("Minimum occurring character: " . $minChar);  
print("<br>Maximum occurring character: " . $maxChar);  
?>  
</body>  
</html>  

 

Output:

Minimum occurring character: a
Maximum occurring character: e

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


Program to find reverse of a string... >>
<< Program to determine whether one string is a rotat...