home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 316 / libsrc / atoi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  523 b   |  29 lines

  1.  
  2. int atoi (str)
  3. char * str;
  4. {
  5.   int value = 0;        /* accumulated value */
  6.   int sgn = 0;            /* if seen sign */
  7.   char c;
  8.  
  9. /* first skip whitespace */
  10.   for ( ; (c = *str++) ; )
  11.     if (!((c == ' ') || (c == '\t')))
  12.         break;
  13.   if (c == '+')
  14.     sgn = 1;
  15.     else
  16.   if (c == '-')
  17.     sgn = -1;
  18.     else
  19.     {
  20.     str--;        /* back up so loop will catch it again */
  21.     sgn = 1;
  22.     }
  23. /* process digits */
  24.   for ( ; ((c = *str++) && (c >= '0') && (c <= '9')) ; )
  25.     value = (value * 10) + (c - '0');
  26.  
  27.   return(value * sgn);
  28. }
  29.