Permalink
Fetching contributors…
Cannot retrieve contributors at this time
34 lines (30 sloc) 908 Bytes
/*
* Filename: atoi.c
* Author: Thomas van der Burgt <thomas@thvdburgt.nl>
* Date: 04-MAR-2010
*
* The C Programming Language, second edition,
* by Brian Kernighan and Dennis Ritchie
*
* Exercise 5-6, page 107
*
* Rewrite appropriate programs from earlier chapters and exercises with
* pointers instead of array indexing. Good possibilities include
* getline (Chapters 1 and 4), atoi, itoa, and their variants
* (Chapters 2, 3, and 4), reverse (Chapter 3), and strindex and getop
* (Chapter 4).
*/
#include <ctype.h>
/* atoi: convert string s to integer */
int atoi(const char *s)
{
int n, sign;
while (isspace(*s))
s++; /* skip whitespace */
sign = (*s == '-') ? -1 : 1;
if (*s == '+' || *s == '-') /* skip sign */
s++;
for (n = 0; isdigit(*s); s++)
n = 10 * n + (*s -'0');
return sign * n;
}