Created
February 19, 2019 23:22
-
-
Save carlosascari/1ed0e416ff28104f0e93969acff2978d to your computer and use it in GitHub Desktop.
Find index of a cstring in a cstring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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