Skip to content

Instantly share code, notes, and snippets.

@carlosascari
Created February 19, 2019 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosascari/1ed0e416ff28104f0e93969acff2978d to your computer and use it in GitHub Desktop.
Save carlosascari/1ed0e416ff28104f0e93969acff2978d to your computer and use it in GitHub Desktop.
Find index of a cstring in a cstring
int index_of(char* hay, char* needle, size_t start) {
size_t len = strlen(hay);
size_t needle_len = strlen(needle);
int subfind = 0;
for (int j = start; j < len; ++j)
{
if (hay[j] == needle[0]) {
subfind = 1;
for (int k = 1; k < needle_len; ++k)
{
if (hay[j + k] != needle[k]) {
subfind = 0;
break;
} else {
subfind = 1;
}
}
if (subfind) return j;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment