
/* Kris's version:
char *my_strstr(char *str, char *to_find)
{
  int i;
  int j;
  int size;
  char temp[my_strlen(to_find)];
  size = my_strlen(to_find);
  for (i = 0; i <= (my_strlen(str) - size); i++)
  {
    my_strncpy(temp,str+i,size);
    if (my_strcmp(temp,to_find) == 0)
    {
      return(str+i);
    }
  }
}
*/

#include <string.h>
#include <stdio.h>

char *my_strstr(const char *str, const char *to_find)
{
  while (*str) {
    const char *p = str, *q = to_find;
    while (*p == *q && *p) p++, q++;
    if (*q == 0) return str;
    if (*p == 0) return 0;
    str++;
  }
  return *to_find ? 0 : str;
}

/* Test the my_strstr function
 * try(a, b, x) says that my_strstr(a,b) should return x.
 */
void try(char *a, char *b, char *x_res) 
{
  char *a_res = my_strstr(a, b);
  static test = 1;
  int ok = (a_res == 0 && x_res == 0) || 
    a_res && x_res && strcmp(a_res, x_res) == 0;
  if (ok) printf ("ok %u\n", test++);
  else printf ("not ok %u # %s %s %s\n", test++, a, b, a_res);
}

int main(void) {
  try("needle", "haystack", 0);
  try("foo", "o", "oo");
  try("fool", "f", "fool");
  try("fool", "l", "l");
  try("fool", "t", 0);
  try("fool", "oo", "ool");
  try("fool", "foo", "fool");
  try("fool", "goo", 0);
  try("fool", "ool", "ool");
  try("fool", "fool", "fool");
  try("fool", "ooo", 0);
  try("fool", "foolish", 0);
  try("fool", "", "fool");
  try("", "", "");
}
