strstr implementation Please review my strstr implementation. This is an interview question that does not allow the use of strlen(). Is there a better way than using a boolean below?

#include &lt;iostream&gt;
#include &lt;cstring&gt;
char* my_strstr(char * s1, const char *s2)
{
  if ( s1 == NULL || s2 == NULL )
    return NULL;

  size_t s2Size = 0;

  const char *end = s2;
  while (*end++)
    ++s2Size;

  bool match;
  while ( *s1 )
  {
    match = true;
    for ( int i = 0; i &lt; s2Size ; ++i )
    {
      if ( s1[i] != s2[i] )  
      {
        match = false;
        break;
      }
    }
    if ( match )
      return s1;

    ++s1;
  }
  return NULL;
}

const char* my_strstr(const char * s1, const char *s2)
{
  return my_strstr(const_cast&lt;char*&gt;(s1),s2);
}


int main()
{
  const char str[]= "This is a strstr test";
  const char search[]= "strstr";

  const char *result = my_strstr(str,search);

  if ( result )
    printf("original: %s\n item: %s\n found: %s\n", str, search, result );
}
By counting the length of s2, you missed the point of this exercise.  Technically, you didn't call strlen(), but you emulated it, which is arguably even worse.  The point was to test your proficiency with pointers, and you failed this interview question.  (Why not count the length?  Because it's possible to do it without incurring the cost of that O(n) operation.  The only time you need to walk along the entire length of s2 is when a match is imminent.)

The strstr(3) documentation probably calls the arguments s1 and s2.  However, I'll borrow a rare piece of wisdom from the PHP documentation and call the arguments haystack and needle.

Don't use variables to control the execution flow.  Prefer to use more active means such as continue, break, and return.  If you have detected a match, what are you waiting for?  Celebrate your success with an immediate return!

Your imports are wrong.  The only library function you call is printf(), so you should #include &lt;cstdio&gt;.

Here's a solution.

#include &lt;cstdio&gt;

char* my_strstr(char *haystack, const char *needle) {
    if (haystack == NULL || needle == NULL) {
        return NULL;
    }

    for ( ; *haystack; haystack++) {
        // Is the needle at this point in the haystack?
        const char *h, *n;
        for (h = haystack, n = needle; *h &amp;&amp; *n &amp;&amp; (*h == *n); ++h, ++n) {
            // Match is progressing
        }
        if (*n == '\0') {
            // Found match!
            return haystack;
        }
        // Didn't match here.  Try again further along haystack.
    }
    return NULL;
}

const char* my_strstr(const char *haystack, const char *needle) {
    return my_strstr(const_cast&lt;char *&gt;(haystack), needle);
}

int main() {
    const char *str = "This is a strstr test";
    const char *search = "strstr";

    const char *result = my_strstr(str, search);

    if (result)
        printf("original: %s\n item: %s\n found: %s\n", str, search, result);
    return 0;
}