Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /*                                                                            */
  3. /*                                                        :::      ::::::::   */
  4. /*   ft_atoi.c                                          :+:      :+:    :+:   */
  5. /*                                                    +:+ +:+         +:+     */
  6. /*   By: sgusache <sgusache@student.42.fr>          +#+  +:+       +#+        */
  7. /*                                                +#+#+#+#+#+   +#+           */
  8. /*   Created: 2018/12/02 19:05:13 by sgusache          #+#    #+#             */
  9. /*   Updated: 2018/12/11 11:39:38 by sgusache         ###   ########.fr       */
  10. /*                                                                            */
  11. /* ************************************************************************** */
  12.  
  13. #include "libft.h"
  14.  
  15. static int      ft_countint(char *str)
  16. {
  17.     int i;
  18.     int count;
  19.  
  20.     count = 0;
  21.     i = 0;
  22.     while (str[i])
  23.     {
  24.         if (ft_isdigit(str[i]) && str[i] != '0')
  25.             count++;
  26.         i++;
  27.     }
  28.     return (count);
  29. }
  30.  
  31. int             ft_atoi(char *str)
  32. {
  33.     int     i;
  34.     int     is_neg;
  35.     int     number;
  36.  
  37.     i = 0;
  38.     is_neg = 0;
  39.     number = 0;
  40.     while ((str[i] == ' ') || (str[i] == '\t') || (str[i] == '\n')
  41.         || (str[i] == '\v') || (str[i] == '\f') || (str[i] == '\r'))
  42.        
  43.         i++;
  44.     if (ft_countint((char*)&str[i]) > 11)
  45.         return ((str[0] == '-') ? 0 : -1);
  46.     if (str[i] == '-')
  47.         is_neg = 1;
  48.     if ((str[i] == '-') || (str[i] == '+'))
  49.         i++;
  50.     while ((str[i] >= '0') && (str[i] <= '9'))
  51.     {
  52.         number *= 10;
  53.         number += ((int)str[i] - '0');
  54.         i++;
  55.     }
  56.     return ((is_neg) ? (-number) : (number));
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement