Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /*                                                                            */
  3. /*                                                        :::      ::::::::   */
  4. /*   ft_atoi.c                                          :+:      :+:    :+:   */
  5. /*                                                    +:+ +:+         +:+     */
  6. /*   By: sgusache <sgusache@student.42.fr>          +#+  +:+       +#+        */
  7. /*                                                +#+#+#+#+#+   +#+           */
  8. /*   Created: 2018/11/09 12:56:06 by sgusache          #+#    #+#             */
  9. /*   Updated: 2018/11/13 18:56:27 by sgusache         ###   ########.fr       */
  10. /*                                                                            */
  11. /* ************************************************************************** */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. int ft_atoi(char *str)
  17. {
  18.     int i = 0;
  19.     int sign = 1;
  20.     int number = 0;
  21.     if(!str)
  22.         return (0);
  23.     while (str[i]==' ' || str[i]=='\t'|| str[i] =='\n'||str[i]=='\r'||str[i]=='\f'||str[i]=='\v'|| str[i]=='+')
  24.         i++;
  25.     if (str[i] == '-')
  26.         {
  27.             sign = -1;
  28.         i++;
  29.         }
  30.     while (str[i]>= '0' && str[i] <= '9')
  31.     {
  32.          if (str[i] >= '0' && str[i] <= '9')
  33.         {
  34.             number *= 10;
  35.             number += str[i] - '0';
  36.         }
  37.         else
  38.             break ;
  39.         i++;
  40.     }
  41.     return ((sign == -1) ? (-number) : (number));
  42. }
  43.  
  44.  
  45. int main(int argc, char **argv)
  46. {
  47.     if(argc != 2)
  48.         return 0;
  49.     printf("RESULT OF MY FUNC = %d\n",ft_atoi(argv[1]));
  50.     printf("lIB result = %d\n",atoi(argv[1]));
  51. //ft_putnbr(ft_atoi(argv[1]));
  52.  return(0);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement