Input a string & character to be searched by the user. Search the character in the string and count the number of times it appears in the string and also display the indexes at which it occurs. code example

Example: c program to the count the number of times each character appears

#include <stdio.h>
int main() {
    char str[1000], ch;
    int count = 0;

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

    printf("Enter a character to find its frequency: ");
    scanf("%c", &ch);

    for (int i = 0; str[i] != '\0'; ++i) {
        if (ch == str[i])
            ++count;
    }

    printf("Frequency of %c = %d", ch, count);
    return 0;
}

Tags:

Java Example