0

How do I find the frequency of all the characters and not only one character (using functions and strings)? Example input: aaaaabbccc Expected output: 'a' : 5 'b' : 2 'c' : 3

#include <stdio.h>
int main() {
    char string[1000], character;
    int amount = 0;

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

    printf("Enter any character from the word to find the frequency of the character: ");
    scanf("%c", &character);

    for (int i = 0; string[i] != '\0'; ++i) {
        if (character == string[i])
            ++amount;
    }

    printf("Frequency of %c = %d", character, amount);
    return 0;
}

0 Answers0