char *strstr( char *haystack, char *needle ) { char *hp; char *np; if( !*needle ) return haystack; // null needle; return haystack (strange POSIX specification requirement!) while( *haystack ) { // until we reach the end of the haystack hp = haystack; // copy of the haystack pointer np = needle; // copy of the needle pointer do { if( !*np ) return haystack; // end of needle; match completed; return current value of haystack pointer } while( *hp++ == *np++ ); // increment both pointer (copies) while the chars match ++haystack; // Got here means a mismatch; base pointer to next character. } return 0; // Got here means we hit the terminating null of the haystack; no match; return null. }