ft_atoi.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_atoi.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@students.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2015/11/23 17:07:30 by bchanot #+# #+# */
  9. /* Updated: 2015/12/29 16:18:37 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libft.h"
  13. int ft_atoi(const char *str)
  14. {
  15. int cpt;
  16. int nb;
  17. int neg;
  18. cpt = 0;
  19. neg = 0;
  20. nb = 0;
  21. while (str[cpt] == ' ' || str[cpt] == '\n' || str[cpt] == '\v' ||
  22. str[cpt] == '\r' || str[cpt] == '\t' || str[cpt] == '\f')
  23. cpt++;
  24. if (str[cpt] == '-')
  25. neg = 1;
  26. if (str[cpt] == '+' || str[cpt] == '-')
  27. cpt++;
  28. while (str[cpt] >= '0' && str[cpt] <= '9')
  29. {
  30. nb *= 10;
  31. nb += (str[cpt] - 48);
  32. cpt++;
  33. }
  34. return (neg == 1 ? -nb : nb);
  35. }