How to implement the strchr function in C

1 Answer

0 votes
#include <stdio.h>

char* strchr(const char* s, const char ch) {
	// find first occurrence of ch in s
	for (; *s != ch; s++)
		if (*s == '\0')
			return (NULL);
	return ((char*)s);
}

int main()
{
	char str[64] = "c programming";

	puts(strchr(str, 'm'));

	return 0;
}





/*
run:
         
mming
      
*/

 



answered Dec 16, 2022 by avibootz

Related questions

1 answer 136 views
1 answer 224 views
224 views asked Jan 11, 2020 by avibootz
1 answer 104 views
104 views asked Aug 16, 2024 by avibootz
1 answer 85 views
85 views asked Jun 10, 2025 by avibootz
1 answer 132 views
...