ft_strnstr: initial implementation
This commit is contained in:
parent
1c3df31518
commit
0a1e2f2b37
3 changed files with 66 additions and 2 deletions
61
ft_strnstr.c
Normal file
61
ft_strnstr.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strnstr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/16 15:38:24 by kcolin #+# #+# */
|
||||
/* Updated: 2024/10/16 15:58:22 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strnstr(const char *big, const char *little, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
int found;
|
||||
|
||||
if (*little == '\0')
|
||||
return ((char *)big);
|
||||
i = 0;
|
||||
while (big[i] != '\0' && i < len)
|
||||
{
|
||||
j = 0;
|
||||
found = 1;
|
||||
while (little[j] != '\0' && i + j < len)
|
||||
{
|
||||
if (little[j] != big[i + j])
|
||||
{
|
||||
found = 0;
|
||||
break ;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
if (found == 1 && little[j] == '\0')
|
||||
return ((char *)big + i);
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
#include <stdio.h>
|
||||
#include <bsd/string.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
size_t cmp_len;
|
||||
|
||||
cmp_len = 40;
|
||||
if (argc > 2)
|
||||
{
|
||||
printf("mine: %s\nlib: %s\n",
|
||||
ft_strnstr(argv[1], argv[2], cmp_len),
|
||||
strnstr(argv[1], argv[2], cmp_len));
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue