Skip to content

Instantly share code, notes, and snippets.

@Riketta
Created July 17, 2015 14:25
Show Gist options
  • Save Riketta/eec4167bc45aadde46f7 to your computer and use it in GitHub Desktop.
Save Riketta/eec4167bc45aadde46f7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int my_strlen(char *str);
void my_strcpy(char *source, char *target);
int my_strstr(char *haystack, char *needle);
int main(int argc, char *argv[])
{
if (argc == 1)
{
printf("Enter your string:\n");
//gets_s(*argv);
}
char *wut = "123wuwut456";
printf("Length: %i\n", my_strlen(wut));
char *needle = "wut";
printf("Offset: %d\n", my_strstr(wut, needle));
return 0;
}
int my_strlen(char *str)
{
char *s;
for (s = str; *s; ++s);
return (s - str); // / sizeof(char)
}
void my_strcpy(char *source, char *target)
{
}
int my_strstr(char *haystack, char *needle)
{
int offset = -1;
//for (s = str; *s; ++s);
char *_haystack;
char *_needle = needle; // wtf
for (_haystack = haystack; *_haystack, *_needle; ++_haystack)
{
if (*_haystack == *_needle && offset == -1)
offset = _haystack;
if (offset != -1 && *_needle)
{
if (*_haystack != *_needle)
{
_haystack = offset + 1; // rly?
_needle = needle;
offset = -1;
}
else
++_needle;
}
}
if (offset != -1)
return offset - (int)haystack;
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment