ft_atoi.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_atoi.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: mazimi <mazimi@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2014/11/03 15:35:21 by mazimi #+# #+# */
  9. /* Updated: 2015/02/18 23:38:04 by mazimi ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "../libft.h"
  13. int ft_atoi(char *str)
  14. {
  15. int nb;
  16. int i;
  17. int neg;
  18. nb = 0;
  19. i = 0;
  20. neg = 1;
  21. while ((str[i] >= 9 && str[i] <= 13) || str[i] == ' ')
  22. i++;
  23. if (str[i] == '-')
  24. {
  25. neg = -1;
  26. i++;
  27. }
  28. else if (str[i] == '+')
  29. i++;
  30. while (str[i] && ft_isdigit(str[i]) == 1)
  31. nb = nb * 10 + str[i++] - 48;
  32. return (neg * nb);
  33. }