Q:

Program to find the frequency of characters

belongs to collection: String Programs

0

Explanation

In this program, we need to find the frequency of each character present in the word.

Picture perfect  

To accomplish this task, we will maintain an array called freq with same size of the length of the string. Freq will be used to maintain the count of each character present in the string. Now, iterate through the string to compare each character with rest of the string. Increment the count of corresponding element in freq. Finally, iterate through freq to display the frequencies of characters.

For example: Frequency of p in above string is 2.

Algorithm

  1. Define a string.
  2. Define an array freq with the same size of the string.
  3. Two loops will be used to count the frequency of each character. Outer loop will be used to select a character and initialize element at corresponding index in array freq with 1.
  4. Inner loop will compare the selected character with rest of the characters present in the string.
  5. If a match found, increment element in freq by 1 and set the duplicates of selected character by '0' to mark them as visited.
  6. Finally, display the character and their corresponding frequencies by iterating through the array freq.

nput:

string = "picture perfect"  

Output:

Characters and their corresponding frequencies
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1

All Answers

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

Python

string = "picture perfect";  
freq = [None] * len(string);  
   
for i in range(0, len(string)):  
    freq[i] = 1;  
    for j in range(i+1, len(string)):  
        if(string[i] == string[j]):  
            freq[i] = freq[i] + 1;  
              
            #Set string[j] to 0 to avoid printing visited character  
            string = string[ : j] + '0' + string[j+1 : ];  
              
#Displays the each character and their corresponding frequency  
print("Characters and their corresponding frequencies");  
for i in range(0, len(freq)):  
    if(string[i] != ' ' and string[i] != '0'):  
        print(string[i] + "-" + str(freq[i]));  

 

Output:

 Characters and their corresponding frequencies
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1

 

C

#include <stdio.h>  
#include <string.h>  
   
int main()  
{  
    char string[] = "picture perfect";  
    int i, j, length = strlen(string);  
    int freq[length];  
      
      
    for(i = 0; i < strlen(string); i++) {  
        freq[i] = 1;  
        for(j = i+1; j < strlen(string); j++) {  
            if(string[i] == string[j]) {  
                freq[i]++;  
                  
                //Set string[j] to 0 to avoid printing visited character  
                string[j] = '0';  
            }  
        }  
    }  
      
    //Displays the each character and their corresponding frequency  
    printf("Characters and their corresponding frequencies\n");  
    for(i = 0; i < length; i++) {  
        if(string[i] != ' ' && string[i] != '0')  
            printf("%c-%d\n", string[i], freq[i]);  
    }  
          
    return 0;  
}  

 

Output:

Characters and their corresponding frequencies: 
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1

 

JAVA

public class Frequency   
{  
     public static void main(String[] args) {  
        String str = "picture perfect";  
        int[] freq = new int[str.length()];  
        int i, j;  
          
        //Converts given string into character array  
        char string[] = str.toCharArray();  
          
        for(i = 0; i <str.length(); i++) {  
            freq[i] = 1;  
            for(j = i+1; j <str.length(); j++) {  
                if(string[i] == string[j]) {  
                    freq[i]++;  
                      
                    //Set string[j] to 0 to avoid printing visited character  
                    string[j] = '0';  
                }  
            }  
        }  
          
        //Displays the each character and their corresponding frequency  
        System.out.println("Characters and their corresponding frequencies");  
        for(i = 0; i <freq.length; i++) {  
            if(string[i] != ' ' && string[i] != '0')  
                System.out.println(string[i] + "-" + freq[i]);  
        }  
    }  
}  

 

Output:

Characters and their corresponding frequencies
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1 

 

C#

using System;  
   
public class Frequency  
{      
    public static void Main()  
    {  
        String str = "picture perfect";  
        int[] freq = new int[str.Length];  
        int i, j;  
          
        //Converts given string into character array  
        char[] string1 = str.ToCharArray();  
          
        for(i = 0; i <str.Length; i++) {  
            freq[i] = 1;  
            for(j = i+1; j <str.Length; j++) {  
                if(string1[i] == string1[j]) {  
                    freq[i]++;  
                      
                    //Set string1[j] to 0 to avoid printing visited character  
                    string1[j] = '0';  
                }  
            }  
        }  
          
        //Displays the each character and their corresponding frequency  
        Console.WriteLine("Characters and their corresponding frequencies");  
        for(i = 0; i < freq.Length; i++) {  
            if(string1[i] != ' ' && string1[i] != '0')  
                Console.WriteLine(string1[i] + "-" + freq[i]);  
        }  
    }  
}  

 

Output:

Characters and their corresponding frequencies: 
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1 

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
$string = "picture perfect";   
$freq = array();  
   
for($i = 0; $i < strlen($string); $i++) {  
    array_push($freq, 1);  
    for($j = $i+1; $j <strlen($string); $j++) {  
        if($string[$i] == $string[$j]) {  
            $freq[$i]++;  
              
            //Set $string[$j] to 0 to avoid printing visited character  
            $string[$j] = '0';  
        }  
    }  
}  
   
//Displays the each character and their corresponding frequency  
print("Characters and their corresponding frequencies<br>");  
for($i = 0; $i < count($freq); $i++) {  
    if($string[$i] != ' ' && $string[$i] != '0'){  
        print($string[$i] . "-" . $freq[$i]);  
        print("<br>");  
    }  
}  
?>  
</body>  
</html>  

 

Output:

Characters and their corresponding frequencies:
p-2
i-1
c-2
t-2
u-1
r-2
e-3
f-1

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 the largest & smallest word in a s... >>
<< Program to find the duplicate words in a string...