Character Frequency Map

Character Frequency Map

Hard C Strings 32 views
Explanation Complexity

Problem Statement

Count the frequency of each character in the string. Create an array of size 256 (his). Then print only the non-zero frequencies.

Input Format

A string entered by the user.

Output Format

Each character with its frequency (only non-zero counts).

Example

"hello"
h : 1
e : 1
l : 2
o : 1

Constraints

• Character set size = 256

• String can contain any characters

Concept Explanation

An array of size 256 is used where each index represents one character.
The count at that index stores how many times the character appears.

Step-by-Step Explanation

1.Create an integer array freq[256] and initialize all values to 0.

2.Traverse the input string character by character.

3.For each character ch:

• Convert it to its ASCII value.

• Increment freq[ASCII value].

4.After traversal, loop through index 0 to 255.

5.If freq[i] > 0:

• Print the character (char)i and its frequency.

6.Ignore indices with frequency 0.

Concept Explanation

An array of size 256 is used where each index represents one character.
The count at that index stores how many times the character appears.

Step-by-Step Explanation

1.Create an integer array freq[256] and initialize all values to 0.

2.Traverse the input string character by character.

3.For each character ch:

• Convert it to its ASCII value.

• Increment freq[ASCII value].

4.After traversal, loop through index 0 to 255.

5.If freq[i] > 0:

• Print the character (char)i and its frequency.

6.Ignore indices with frequency 0.

Input / Output Format

Input Format
A string entered by the user.
Output Format
Each character with its frequency (only non-zero counts).
Constraints
• Character set size = 256

• String can contain any characters

Examples

Input:
"hello"
Output:
h : 1 e : 1 l : 2 o : 1

Example Solution (Public)

C
#include <stdio.h>

int main() {
    char str[200];
    int freq[256] = {0};   // histogram array
    int i;

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

    // Count frequency of each character
    for (i = 0; str[i] != '�'; i++) {
        unsigned char ch = str[i];
        freq[ch]++;
    }

    // Print non-zero frequencies
    printf("nCharacter frequencies:n");
    for (i = 0; i < 256; i++) {
        if (freq[i] > 0) {
            if (i == 'n')
                printf("n : %dn", freq[i]);
            else if (i == ' ')
                printf("space : %dn", freq[i]);
            else
                printf("%c : %dn", i, freq[i]);
        }
    }

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    char str[200];
    int freq[256] = {0};   // histogram array
    int i;

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

    // Count frequency of each character
    for (i = 0; str[i] != '�'; i++) {
        unsigned char ch = str[i];
        freq[ch]++;
    }

    // Print non-zero frequencies
    printf("nCharacter frequencies:n");
    for (i = 0; i < 256; i++) {
        if (freq[i] > 0) {
            if (i == 'n')
                printf("n : %dn", freq[i]);
            else if (i == ' ')
                printf("space : %dn", freq[i]);
            else
                printf("%c : %dn", i, freq[i]);
        }
    }

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.