Bird
0
0
DSA Cprogramming~10 mins

Character Frequency Counting in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an array to store frequency of all ASCII characters.

DSA C
int freq[[1]] = {0};
Drag options to blanks, or click blank then click option'
A64
B256
C512
D128
Attempts:
3 left
💡 Hint
Common Mistakes
Using 128 instead of 256, which misses extended ASCII characters.
Using too small or too large array size.
2fill in blank
medium

Complete the code to increment the frequency count of the current character in the string.

DSA C
freq[(unsigned char)[1]]++;
Drag options to blanks, or click blank then click option'
Astr[i]
Bi
Cch
Dc
Attempts:
3 left
💡 Hint
Common Mistakes
Using the index variable i directly instead of the character.
Using an undefined variable.
3fill in blank
hard

Fix the error in the loop condition to iterate over the string until the end.

DSA C
for (int i = 0; str[i] [1] '\0'; i++) {
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which stops immediately.
Using '<' or '>' which is invalid for char comparison here.
4fill in blank
hard

Fill both blanks to print characters and their frequencies only if frequency is greater than zero.

DSA C
for (int i = 0; i < [1]; i++) {
    if (freq[i] [2] 0) {
        printf("%c: %d\n", i, freq[i]);
    }
}
Drag options to blanks, or click blank then click option'
A256
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Looping less than 256 characters.
Checking frequency with '==' instead of '>'.
5fill in blank
hard

Fill all three blanks to create a function that counts character frequency and prints the result.

DSA C
void countFrequency(const char *[1]) {
    int freq[[2]] = {0};
    for (int i = 0; [3][i] != '\0'; i++) {
        freq[(unsigned char)[3][i]]++;
    }
    for (int i = 0; i < 256; i++) {
        if (freq[i] > 0) {
            printf("%c: %d\n", i, freq[i]);
        }
    }
}
Drag options to blanks, or click blank then click option'
Astr
B256
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Wrong array size.
Incorrect loop variable.