Skip to content

Instantly share code, notes, and snippets.

@dermesser
Created September 19, 2014 18:34
Show Gist options
  • Save dermesser/2af65f55c0be32e8fb9f to your computer and use it in GitHub Desktop.
Save dermesser/2af65f55c0be32e8fb9f to your computer and use it in GitHub Desktop.
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <stdbool.h>
_Bool find(const char* haystack, const char* needle)
{
size_t hlen = strlen(haystack);
size_t nlen = strlen(needle);
unsigned int found = 0;
for ( unsigned int i = 0; i < hlen; i++ )
{
found = 0;
for ( unsigned int j = 0; i+j < hlen; j++ )
{
if ( haystack[i+j] != needle[j] )
break;
else
found++;
}
if ( found == nlen )
break;
}
if ( found == nlen )
return true;
else
return false;
}
int main(void)
{
printf("%i\n",find("abcdefg","efgh"));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment