ft_strsub.c 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_strsub.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: mazimi <mazimi@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2014/12/09 13:45:03 by mazimi #+# #+# */
  9. /* Updated: 2016/11/29 16:47:48 by mazimi ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "../libft.h"
  13. char *ft_strsub(char const *s, unsigned int start, size_t len)
  14. {
  15. char *res;
  16. size_t i;
  17. if (len <= 0 || s == NULL)
  18. return (ft_strdup(""));
  19. if (len > (size_t)ft_strlen(s))
  20. return (NULL);
  21. res = ft_strnew(len);
  22. if (res == NULL)
  23. return (NULL);
  24. i = -1;
  25. while (++i < len)
  26. res[i] = s[start++];
  27. res[i] = '\0';
  28. return (res);
  29. }