Write Your Own Integer Conversion

It’s not that programmers are lazy. Many professions involve a lot of copy-and-paste, borrowing stuff done before, re-purposing and re-using. Why re-invent the wheel? Still, doing so helps exercise your programming kung-fu as well as offers insights into how things work and why some operations are done the way they are.

As an example, consider the atoi() function.

An older C language function, atoi() converts a string to an integer value. The name implies “ASCII to Integer.” Its current implementation is as a macro for the strtol() function. Both are prototyped in the stdlib.h header.

Your task for this month’s Exercise is to write code that emulates the primary behavior of the atoi() function: convert a string value to an integer.

Don’t worry about leading whitespace.

Don’t fret over a sign before the value (positive or negative).

And don’t frustrate yourself converting alphabetic characters.

In fact, here’s the programming skeleton to get you started:

#include <stdio.h>
#include <stdlib.h>

int convert(char *s)
{
    return(r);
}

int main()
{
    char value[] = "345966";
    int v;

    v = atoi(value);
    printf("%s from atoi() is %d\n",value,v);
    v = convert(value);
    printf("%s from convert() is %d\n",value,v);

    return(0);
}

Your task is to complete the convert() function, which swallows a string value and returns a base 10 (decimal) integer. I can think of a number of ways to solve this Exercise. Click here to view my solutions, but please try devising your own solution before peeking at what I’ve done.

3 thoughts on “Write Your Own Integer Conversion

  1. atoi is so poorly implemented it’s useless unless you are willing to do some form of validation beforehand. And if you are you might as well implement a combined validation/conversion function and forget atoi completely.

    If you have an input string that contains a valid int plus a load of other stuff, decimal fractions, words or whatever then you don’t have valid data and it should be rejected as invalid. And as for returning 0 if the string can’t be converted to an int, who thought that up?

    (Sounded like a rant – sorry!)

    “Many professions involve a lot of copy-and-paste, borrowing stuff done before, re-purposing and re-using.”
    Except lawyers of course…

  2. Depends on the lawyer. Setting up a will or a corporation is mostly copy-and-paste. A litigator relies upon case law, which isn’t copy-and-paste but often just as bad.

    Anyhoo…the biggest problem I have with atoi() is typing atio() instead.

  3. My lawyer comment was satirical – should have made that clear!

    On a more general note, some languages or frameworks have an official or de facto standard repository for libraries, components etc. Obviously there is a ton of C code floating around but in a very fragmented way. I am not aware of any attempts to create something like PyPi for C. If there were I think it would be very successful if well run, and would enhance the reputation, usefulness and usage of C considerably.

Leave a Reply