#include <stdio.h> int first_occurrence(char *string, char ch) { int position = -1; int index = 0; while (string[index] != '\0') { if (string[index] == ch) { position = index; break; } index++; } return position; } int main() { char input_string[] = "hello world"; char character_to_find = 'o'; printf("First occurrence of '%c' in '%s': %d\n", character_to_find, input_string, first_occurrence(input_string, character_to_find)); 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