search.c
/// naive approach (for C strings)
naive approach (for C strings)
naive approach ( for C strings )
const char* searchSimpleString(const char* haystack, const char* needle)
{
  if (!haystack || !needle)
    return NULL;
  while (*haystack)
  {
    size_t i = 0;
    while (needle[i])
    {
      if (haystack[i] != needle[i])
        break;
      i++;
    }
    if (!needle[i])
      return haystack;
    haystack++;
  }
  return NULL;
}
const char * searchSimpleString ( const char * haystack , const char * needle ) { if ( ! haystack || ! needle ) return NULL ; while ( * haystack ) { size_t i = 0 ; while ( needle [ i ] ) { if ( haystack [ i ] != needle [ i ] ) break ; i ++ ; } if ( ! needle [ i ] ) return haystack ; haystack ++ ; } return NULL ; }
naive approach (for C strings)
naive approach ( for C strings )
// detect invalid input // until the end of haystack (or a match, of course) // compare current haystack to needle // needle fully matched (reached terminating NUL-byte) // no match, step forward // not found