//Program to find the count of given character in a given string

PHOTO EMBED

Tue May 07 2024 15:14:14 GMT+0000 (Coordinated Universal Time)

Saved by @vedanti

#include <stdio.h>

int count_character(char *string, char ch) {
    int count = 0;
    while (*string != '\0') {
        if (*string == ch) {
            count++;
        }
        string++;
    }
    return count;
}

int main() {
    char input_string[] = "hello world";
    char character_to_count = 'o';
    printf("Count of '%c' in '%s': %d\n", character_to_count, input_string, count_character(input_string, character_to_count));
    return 0;
}
content_copyCOPY