add strstr

This commit is contained in:
apio 2022-09-08 17:02:16 +02:00
parent f3f9756106
commit 9de9eacb1d
2 changed files with 22 additions and 0 deletions

View File

@ -11,6 +11,8 @@ char* strncpy(char* dest, const char* src, size_t n);
int strncmp(const char* s1, const char* s2, size_t n); int strncmp(const char* s1, const char* s2, size_t n);
char* strncat(char* dest, const char* src, size_t n); char* strncat(char* dest, const char* src, size_t n);
char* strstr(char* haystack, char* needle);
void* memcpy(void* dest, const void* src, size_t n); void* memcpy(void* dest, const void* src, size_t n);
void* memset(void* dest, int c, size_t n); void* memset(void* dest, int c, size_t n);
int memcmp(const void* a, const void* b, size_t n); int memcmp(const void* a, const void* b, size_t n);

View File

@ -66,6 +66,26 @@ char* strcat(char* dest, const char* src)
return dest; return dest;
} }
char* strstr(char* haystack, char* needle)
{
size_t needle_size = strlen(needle);
size_t haystack_size = strlen(haystack);
while (*haystack)
{
if (*haystack == *needle)
{
if (needle_size <= haystack_size)
{
if (!strncmp(haystack, needle, needle_size)) return haystack;
}
else { return NULL; }
}
haystack++;
haystack_size--;
}
return NULL;
}
void* memcpy(void* dest, const void* src, size_t n) void* memcpy(void* dest, const void* src, size_t n)
{ {
for (size_t i = 0; i < n; ++i) { *((char*)dest + i) = *((char*)src + i); } for (size_t i = 0; i < n; ++i) { *((char*)dest + i) = *((char*)src + i); }