ft_strnstr.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_strnstr.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@students.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2015/11/26 12:21:03 by bchanot #+# #+# */
  9. /* Updated: 2018/10/29 11:56:09 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libft.h"
  13. char *ft_strnstr(const char *const s1, const char *const s2, size_t n)
  14. {
  15. size_t i;
  16. size_t j;
  17. i = 0;
  18. j = 0;
  19. if (!s2 || !ft_strlen(s2))
  20. return ((char *)s1);
  21. while (s1[i])
  22. {
  23. while (s2[j] == s1[i + j] && i + j < n)
  24. {
  25. if (j == ft_strlen(s2) - 1)
  26. return ((char *)&s1[i]);
  27. j++;
  28. }
  29. j = 0;
  30. i++;
  31. }
  32. return (NULL);
  33. }