ft_strncmp.c 1.3 KB

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