Find the Frequency of Characters in a String

Write a program to Find the Frequency of Characters in a String.
C Program

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

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

    printf("Enter a character : ");
    scanf("%c", &ch);
    int i = 0;
    while (str[i] != '\0')
    {
        if (str[i] == ch)
        {
            freq++;
        }
        i++;
    }
    printf("Frequecy of charcter %c is %d"chfreq);
    return 0;
}

Example:

Enter a string: beingcodeexpert
Enter a character : e

Example:

Frequecy of charcter e is 4


No comments:

Post a Comment