aboutsummaryrefslogtreecommitdiffstats
path: root/ft_atoi.c
blob: 2fe091af92117035a50883ade3098e2198609edd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "libft.h"

int
ft_atoi (const char *nptr)
{
  int sign;
  int result;

  result = 0;
  sign = 1;
  // Skip isspace() characters: space, \t, \n, \v, \f, \r.
  while (*nptr == ' ' || (*nptr >= '\t' && *nptr <= '\r'))
    nptr++;
  if (*nptr == '-' || *nptr == '+')
    {
      if (*nptr == '-')
        sign = -1;
      nptr++;
    }
  while (*nptr >= '0' && *nptr <= '9')
    {
      result = result * 10 + (*nptr - '0');
      nptr++;
    }
  return (result * sign);
}