1. 程式人生 > >【C語言】模擬實現strchr函式.即在一個字串中查詢一個字元第一次出現的位置並返回

【C語言】模擬實現strchr函式.即在一個字串中查詢一個字元第一次出現的位置並返回

//模擬實現strchr函式.即在一個字串中查詢一個字元第一次出現的位置並返回
#include <stdio.h>
//#include <string.h>
#include <assert.h>
char* my_strchr(char *dst, char src)
{
	assert(dst);
	while (*dst != '\0')
	{
		if (*dst == src)
			return dst;
		dst++;
	}
	return 0;
}
int main()
{
	char a[] = "hello world!";
	printf("%s\n", my_strchr(a, 'l'));
	//printf("%s\n", strchr(a, 'e'));
	return 0;
}