diff --git a/ncc/include/string.h b/ncc/include/string.h index bb54b96..f5706af 100644 --- a/ncc/include/string.h +++ b/ncc/include/string.h @@ -68,10 +68,34 @@ char* strchr(char *str, int c) return NULL; } -// TODO: // Returns a pointer to the first occurrence of str2 in str1, // or a null pointer if str2 is not part of str1. -// char* strstr(char * str1, const char * str2); +char* strstr(char* s1, char* s2) +{ + char* p1 = s1; + char* p2; + + while (*s1) + { + p2 = s2; + + while (*p2 && (*p1 == *p2)) + { + ++p1; + ++p2; + } + + if (!*p2) + { + return (char*)s1; + } + + ++s1; + p1 = s1; + } + + return NULL; +} // TODO: // char* strncpy(char* destination, const char* source, size_t num) diff --git a/ncc/tests/strings.c b/ncc/tests/strings.c index 1ce2e6e..b7bb550 100644 --- a/ncc/tests/strings.c +++ b/ncc/tests/strings.c @@ -28,10 +28,19 @@ int main() // strchr char* str = "lestring"; - assert(strchr("", 'c') == null); - assert(strchr("c", 'c') != null); + assert(strchr("", 'c') == NULL); + assert(strchr("c", 'c') != NULL); assert(strchr(str, 's') == str + 2); + // strstr + char* s = "abcabcabcdabcde"; + assert(strstr(s, "x") == NULL); + assert(strstr(s, "xyz") == NULL); + assert(strstr(s, "a") == s + 0); + assert(strstr(s, "abc") == s + 0); + assert(strstr(s, "abcd") == s + 6); + assert(strstr(s, "abcde") == s + 10); + // memset memset(arr, 177, 19); assert(arr[0] == 177);