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 146 views
1 answer 230 views
230 views asked Jan 11, 2020 by avibootz
1 answer 112 views
112 views asked Aug 16, 2024 by avibootz
1 answer 88 views
88 views asked Jun 10, 2025 by avibootz
1 answer 139 views
...