Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int atoi(const char *str)
  4. {
  5.     int n = 0; // nombre parsé
  6.     char c = '0'; // caractère courant
  7.     int i = 0; // itérateur
  8.     int signe = 1;
  9.     if(str[0] == '-')
  10.     {
  11.         signe = -1;
  12.         ++i; // ++i est plus rapide que i++
  13.     }
  14.     while(c >= '0' && c <= '9')
  15.     {
  16.         n *= 10;
  17.         n += c - '0';
  18.         c = str[i++];
  19.     }
  20.     return n*signe;
  21. }
  22.  
  23. int main(void)
  24. {
  25.     const char* str = "-24987";
  26.     int n = atoi(str);
  27.     printf("%s = %d\n", str, n);
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement