Created
May 30, 2017 05:24
-
-
Save CreaturePhil/a98c421d4b118bfe31c5b76dfd5fa83c to your computer and use it in GitHub Desktop.
This file contains 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
char *strstr4(char *payload, char *token) { | |
int found = 0; | |
char *current = token; | |
while (*payload != '\0') { | |
if (*payload == *current) { | |
current++; | |
if (*current == '\0') { | |
found = 1; | |
break; | |
} | |
} else { | |
current = token; | |
} | |
payload++; | |
} | |
if (!found) | |
return NULL; | |
else | |
return payload; | |
} | |
int indexOf(char *str, char *token) { | |
int i = 0; | |
int start = 0; | |
char *ot = token; | |
while (str[i] != '\0') { | |
if (str[i] == *token) { | |
if (strcmp(token, ot) == 0) | |
start = i; | |
token++; | |
if (*token == '\0') { | |
return start; | |
} | |
} else { | |
token = ot; | |
} | |
i++; | |
} | |
return -1; | |
} | |
int main() { | |
char *s = "Hello World!"; | |
char *token = "Wor"; | |
char *res; | |
res = strstr4(s, token); | |
printf("%s\n", res); | |
res = strstr4(s, "za"); | |
if (res == NULL) { | |
printf("Not found\n"); | |
} else { | |
printf("%s\n", res); | |
} | |
res = strstr4(s, token); | |
printf("s2: %s\n", res); | |
res = strstr4(s, "H"); | |
printf("s2: %s\n", res); | |
printf("%d\n", indexOf("Hi", "i")); | |
printf("%d\n", indexOf("Hi", "z")); | |
printf("%d\n", indexOf("big powers | yolo string.zaajsd", "g.z")); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment