Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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