Answer to Question #301192 in C for Suvedhaa

Question #301192

Find the Frequency of Characters. The string entered by the user is stored in variable str. Then, the user is asked to enter the character whose frequency is to be found and display.


Runtime Input :


ksrct r


Output :


1

1
Expert's answer
2022-02-22T12:13:31-0500
#include <stdio.h>

int freq_of_char(char* str, char ch) {
    int count = 0;
    while (*str) {
        if (*str == ch) {
            count++;
        }
        str++;
    }
    return count;
}

int main() {
    char str[256];
    char ch;


    scanf("%s", str);
    scanf(" %c", &ch);

    int frq = freq_of_char(str, ch);
    printf("%d\n", frq);

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS