V
V
Vladimir Rudometkin2019-02-05 13:11:16
C++ / C#
Vladimir Rudometkin, 2019-02-05 13:11:16

Why do you think this code doesn't work?

#include <unistd.h>
#include <stdio.h>
int		ft_atoi(char *str);

int		ft_atoi(char *str)
{
  int res, i, sign;
  
  i = 0;
  sign = 1;
  res = 0;

    if (str[0] == '-')
      {
  sign = -1;
  i++;
      }
    
    while (str[i] >= 48 && str[i] <= 57)

      {
  res = res*10 + (str[i] - '0');
      }

    
    return sign*res;
}

int		main(void)
{
  char str[] = "23";

  int rslt = ft_atoi(str);

  printf("%d", rslt);

  return (0);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-02-05
@spezcial

I don't see something that you increase the value of i in a loop.

R
Rsa97, 2019-02-05
@Rsa97

Everything works exactly as you wrote in the program. The cycle never ends.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question