#include <stdio.h>
#include <string.h>
size_t substringMatching(const char* haystack, const char* needle)
{
size_t haystack_len = strlen(haystack);
size_t needle_len = strlen(needle);
printf("haystack_len:%zu\n", haystack_len);
printf("needle_len:%zu\n", needle_len);
for (size_t i = haystack_len; i >= needle_len; --i)
{
size_t j;
for (j = 0; j < needle_len; ++j)
{
if ((char)haystack[i - needle_len + j] != (char)needle[j])
{
break;
}
}
if (j == needle_len) {
return i - needle_len;
}
}
return (size_t)-1;
}
int main()
{
const char* haystack = "sdc\x0D\x0A\x4F\x4B\x00sac\xFF\xAB\xCD";
const char* needle = "OK";
size_t result = substringMatching(haystack, needle);
if (result != (size_t)-1)
{
printf("Substring found at position: %zu\n", result);
}
else
{
printf("Substring not found.\n");
}
return 0;
}