HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

strstr implementation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
implementationstrstrstackoverflow

Problem

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 
#include 
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 (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 );
}

Solution

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 .

Here's a solution.

#include 

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 && *n && (*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(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;
}

Code Snippets

#include <cstdio>

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 && *n && (*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<char *>(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;
}

Context

StackExchange Code Review Q#35396, answer score: 12

Revisions (0)

No revisions yet.