74 lines
1.7 KiB
C
74 lines
1.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_strstr.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/06/11 14:58:08 by whaffman #+# #+# */
|
|
/* Updated: 2024/06/11 14:58:17 by whaffman ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int compare(char *str, char *to_find)
|
|
{
|
|
int found;
|
|
|
|
found = 0;
|
|
while (*to_find)
|
|
{
|
|
if (*str != *to_find)
|
|
{
|
|
found = 0;
|
|
break ;
|
|
}
|
|
else
|
|
{
|
|
str++;
|
|
to_find++;
|
|
found = 1;
|
|
}
|
|
}
|
|
return (found);
|
|
}
|
|
|
|
char *ft_strstr(char *str, char *to_find)
|
|
{
|
|
int found;
|
|
|
|
if (!*to_find)
|
|
return (str);
|
|
found = 0;
|
|
while (*str)
|
|
{
|
|
if (compare(str, to_find))
|
|
return (str);
|
|
str++;
|
|
}
|
|
return (NULL);
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
void test(char *str, char *to_find)
|
|
{
|
|
printf("haystack: \"%s\"\nneedle: \"%s\"\n", \
|
|
str, to_find);
|
|
printf("index(ft_strstr): %ld\nindex(strstr): %ld\n", \
|
|
ft_strstr(str, to_find) - str, strstr(str, to_find) - str);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
test("Hello, World!", "o, ");
|
|
test("Hello, World!", "o");
|
|
test("Hello, World!", "");
|
|
test("Hello, World!", "H");
|
|
test("Hello, World!", "Z");
|
|
return (0);
|
|
}
|
|
#endif
|