//Write a function to find the position of First occurrence of the input // character in a given string.
Tue May 07 2024 15:15:39 GMT+0000 (Coordinated Universal Time)
Saved by
@vedanti
#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;
}
content_copyCOPY
Comments