Created
October 30, 2015 21:08
-
-
Save Riketta/1d19ccaa622e55fe7168 to your computer and use it in GitHub Desktop.
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
#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() | |
{ | |
char *wut = "123wuwut456\0"; | |
int len = my_strlen(wut); | |
printf("Length: %i\n", len); | |
char *needle = "wut\0"; | |
printf("Offset: %d\n", my_strstr(wut, needle)); | |
char *out = (char*)calloc(len, sizeof(char*)); | |
my_strcpy(wut, out); | |
printf("Copy out: %s\n", out); | |
return 0; | |
} | |
int my_strlen(char *str) | |
{ | |
char *s; | |
for (s = str; *s; ++s); | |
return (s - str); | |
} | |
void my_strcpy(char *source, char *target) | |
{ | |
char* _target = target; | |
for (char *s = source; *s; ++s, ++_target) | |
*_target = *s; | |
} | |
int my_strstr(char *haystack, char *needle) | |
{ | |
size_t offset = -1; | |
char *_haystack; | |
char *_needle = needle; | |
for (_haystack = haystack; *_haystack, *_needle; ++_haystack) | |
{ | |
if (*_haystack == *_needle && offset == -1) | |
offset = _haystack; | |
if (offset != -1 && *_needle) | |
{ | |
if (*_haystack != *_needle) | |
{ | |
_haystack = offset + 1; | |
_needle = needle; | |
offset = -1; | |
} | |
else | |
++_needle; | |
} | |
} | |
if (offset != -1) | |
return offset - (size_t)haystack; | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment