Custom strstr function

Simple replacement for strstr() function present in C standard library

char * custom_strstr(char *source, char *pattern, uint16_t maxbuf_size)
{
	uint8_t pattern_lenght = 0;
	uint32_t i;
	uint32_t cnt = 0;
	char *ptr=NULL;

	pattern_lenght = strlen(pattern);
	for(i=0;i<maxbuf_size;i++){
		if(source[i]==pattern[cnt]){
			cnt++;
			if(cnt==pattern_lenght){
				ptr = source+i-1;
				break;
			}
		}
		else
			cnt = 0;
	}
	return(ptr);
}

Note: The function strlen() also can be replaced by custom function.

Michał Wołowik

Software, hardware embedded engineer. Current duties: Linux(YOCTO) and Android(AOSP) images preparation. Main duties are to develop c/c++ application.

Leave a Reply