Skip to content

Instantly share code, notes, and snippets.

@CreaturePhil
Created May 30, 2017 05:24
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 CreaturePhil/a98c421d4b118bfe31c5b76dfd5fa83c to your computer and use it in GitHub Desktop.
Save CreaturePhil/a98c421d4b118bfe31c5b76dfd5fa83c to your computer and use it in GitHub Desktop.
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