Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Coding an atoi( ) function

Posted on 1998-12-08
7
1,484 Views
Last Modified: 2012-06-27
How do I code my own atoi() function?
0
Comment
Question by:bfields
7 Comments
 
LVL 1

Expert Comment

by:billyh
ID: 1179679
Why code your own function, it will take you valuable time and its more likely to have bugs. It's better to just use atoi as it has been tested and developed by good programmers.

Or may there is something more that you want that atoi does not offer you?
0
 

Author Comment

by:bfields
ID: 1179680
As a problem-solving exercise/brain teaser I would like to code my own.  For starters just taking an integer and getting it into a string without using atoi, etc...
0
 
LVL 1

Accepted Solution

by:
billyh earned 50 total points
ID: 1179681
Here is a code I have just written. Another thing atoi takes a string and converts it to an integer.

The function takes a string and then converts it to an unsigned integer.

That is range of input is from 0 to 4294967295. This is because an unsigned integer is 32-bits long.

Here is the function:

unsigned int my_own(char* str_var)
{
      unsigned int local_var = 0, varlen = strlen(str_var), power = 0;

      --varlen;
      for (int index = varlen; index >= 0; index--)
      {
            local_var += ((int)(str_var[index]) - 48) * ((unsigned int)pow(10,power));
            ++power;
      }
      return local_var;
}
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 84

Expert Comment

by:ozo
ID: 1179682
pow?  to code an atoi function?  homework or not, I can't let that pass without comment

unsigned int my_own(char* str_var){
    unsigned int local_var = 0;
    while( *str_var ){
        local_var *= 10;
        local_var += (int)*str_var++ - '0';
    }
    return local_var;
}
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1179683
You really don't need pow to code atoi(). I guess ozo's solution is more elegant. However, it should check whether the string represents a negative integer ("-1234"). Some more checks to make sure that the character represents a digit is required. Perhaps ozo's code with the following changes would be more meaningful.
     int my_own(char* str_var){
         int local_var = 0,sign;
         sign=(*str_var=='-')?-1:1;
         if(*str_var=='-' || *str_var=='+) str_var++;
         while( isdigit(*str_var) ){
             local_var *= 10;
             local_var += (int)*str_var++ - '0';
         }
         return sign * local_var;
     }

Thanks
pagladasu
0
 

Author Comment

by:bfields
ID: 1179684
This was a good solution.  Two things he should have checked for.  Negative # and non digit characters.
0
 
LVL 1

Expert Comment

by:billyh
ID: 1179685
Guys I accept that when I wrote the program I was not thinking about efficiency, that's why I used pow. You will have to admit that when coding within a 30 minute time, one is bound to make such mistakes.

As for negatives, PLEASE look-up the meaning of unsigned integer. There are no negative numbers in unsigned integers, the first bit which is usually used for the negative numbers is considered as part of the number in unsigned numbers.

That is why 0xffffffff not -1, but 4294967295.

As for non-numberical, I believe bfields said he wanted a brain-teaser. I gave him an idea, now he should enjoy doing the rest.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question