#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; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter