Search⌘ K

Challenge: Locate the First Occurrence

Explore how to implement the strchr function in C by iterating through a string with pointers to find the first occurrence of a specified character. Learn to return the pointer address when the character is found or NULL if not present, reinforcing pointer manipulation and string handling in C.

Introduction

For this challenge, we’re moving away from problems we can solve by a two-pointers approach and instead try something different.

Problem statement

You will have to implement the C library function strchr. This function takes a string and a character as inputs. It returns a pointer to the first occurrence of the character in the string. If the character doesn’t occur in the string, it returns NULL.

The function will look ...