Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

itoa atoi

5 views
Skip to first unread message

Marcelo Cortes

unread,
Oct 21, 2001, 11:21:39 PM10/21/01
to
I have seen many interview question where they ask for a implementation of
itoa and atoi using C/C++. Could anyone send it to me?


Richard Norman

unread,
Oct 21, 2001, 11:24:03 PM10/21/01
to

atoi() is an implementation of atoi using either Standard C or
Standard C++. itoa can be done in a variety of ways, including
sprintf or stringstreams.

Jack Klein

unread,
Oct 21, 2001, 11:29:19 PM10/21/01
to
On Sun, 21 Oct 2001 22:21:39 -0500, "Marcelo Cortes"
<ho...@sympatico.ca> wrote in comp.lang.c++:

> I have seen many interview question where they ask for a implementation of
> itoa and atoi using C/C++. Could anyone send it to me?

Given that itoa() is not a standard library function in either C or
C++, and atoi() is a buggy, dangerous hack by definition, I would be
leery of anyone who used such a question in a job interview situation.

But if I provide source code for you and you get hired because of it,
what percentage of your wages do I get?

If you can't actually write and debug an implementation of either
function inside of 10 minutes on a bad hair day, what makes you think
you are qualified to even interview for a job as a C or C++
programmer?

Finally if this is a sly, sneaky trick on your part and you are
actually trying to get your homework assignment done for you, see
http://home.att.net/~jackklein/ctips01.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

Marcelo Cortes

unread,
Oct 22, 2001, 12:48:54 AM10/22/01
to
Another stupid question: Why did you waste your time answering my post and
my time reading your post?

"Jack Klein" <jack...@spamcop.net> wrote in message
news:mf47ttckldb9loq1v...@4ax.com...

Jack Klein

unread,
Oct 22, 2001, 12:48:39 AM10/22/01
to
On Sun, 21 Oct 2001 23:48:54 -0500, "Marcelo Cortes"
<ho...@sympatico.ca> wrote in comp.lang.c++:

Don't top post, it is against Internet guidelines and considered rude
here.

Amit Dedhia

unread,
Oct 22, 2001, 2:10:29 AM10/22/01
to
#include <stdlib.h>
#include <stdio.h>

void main( void )
{
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;

_itoa( i, buffer, 10 );
printf( "String of integer %d (radix 10): %s\n", i, buffer );
_itoa( i, buffer, 16 );
printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %s\n", i, buffer );

_ltoa( l, buffer, 16 );
printf( "String of long int %ld (radix 16): 0x%s\n", l,
buffer );

_ultoa( ul, buffer, 16 );
printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
buffer );
}


Output

String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2

*******************************************************************************


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

void main( void )
{
char *s; double x; int i; long l;

s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}


Output

atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854


***************************************************************************

"Marcelo Cortes" <ho...@sympatico.ca> wrote in message news:<sdLA7.2643$v21.6...@news20.bellglobal.com>...

Bernd Jochims

unread,
Oct 22, 2001, 12:25:51 PM10/22/01
to
Amit Dedhia wrote:


///

First, posting above the quoted message is considered rude here.

Second, the above code is non-standard and refers to Microsoft run time
library functions. Actually, I am pretty sure it is directly off the
MSDN Library. Complete with non-standard headers and with main returning
void.

Bearing in mind the Original Posters poor attitude and repeated
rudeness, I wonder why I am even bothering to point out the fact that
this code is not only not conforming to modern standard C++, it is also
full of Microsoft specific names.

Third, there is no such computer programming language as c/c++. This ng
is a C++ group. Maybe the OP could get this straight before he posts
here again.


Thomas Hansen

unread,
Oct 23, 2001, 6:33:37 AM10/23/01
to
On Sun, 21 Oct 2001 22:21:39 -0500, there came a drop of sanity from
"Marcelo Cortes" <ho...@sympatico.ca> containing:

>I have seen many interview question where they ask for a implementation of
>itoa and atoi using C/C++. Could anyone send it to me?
>

You can actually do it yourself with a little bit of guidance!
Tip:
Use the modulo operator (%)
Use the division operator (/)
Then if you have a number which you want to convert to a string eg.
123, you can modulo it by 10 which will give you 3 then you can add
that number with the ascii code for the character "0" which is 30 (in
hex) && 48 in Decimal...
Know you have the rightmost character in your string.
Then you divide the number by 100, and if the result of that division
is more then 1 then you do a modulo 100 which will give you "23" which
you can subtract the result from the first operation from (3) and then
you have the 20's etc...

The opposite way (atoi) is even more easy...
--
"FOOT-AND-MOUTH BELIEVED TO BE FIRST VIRUS
UNABLE TO SPREAD THROUGH MICROSOFT OUTLOOK"

Karl Heinz Buchegger

unread,
Oct 23, 2001, 6:41:23 AM10/23/01
to

much to complicated. Use what you have. A C solution
might look something like:

int atoi( const char* str )
{
int res = 0;
sscanf( "%d", &res );
return res;
}

void itoa( int num, const char* str )
{
sprintf( str, "%d", num );
}

--
Karl Heinz Buchegger
kbuc...@gascad.at

Pete Becker

unread,
Oct 23, 2001, 8:52:12 AM10/23/01
to
Thomas Hansen wrote:
>
> Use the modulo operator (%)

That's the remainder operator. C++ does not have a modulus operator. The
difference is that a remainder can be negative, and a modulus cannot.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Marcelo Cortes

unread,
Oct 23, 2001, 10:40:51 PM10/23/01
to
I developed the bellow function for itoa. Could anyone see a way to make it
faster? Does anyone see any mistake?

Thanks

char * MYitoa( int value, char *string, int radix )
{
if(radix < 2)
return "";

bool bMinus = false;
if(value < 0)
{
if(radix != 10)
return "";

string[0] = '-';
value *= -1;
bMinus = true;
}

for(int n = bMinus?1:0; value != 0; n++, value /= radix)
string[n] = (char) ((value % radix) + '0');

string[n] = '\0';

strrev(&string[bMinus?1:0]);

return string;
}


Marcelo Cortes

unread,
Oct 23, 2001, 11:04:33 PM10/23/01
to
The same for atoi...

Thanks again!

int MYatoi( const char *string )
{
bool bMinus = false;
int nRet = 0;

if( string[0] == '-')
{
bMinus = true;
string++;
}

while(*string != '\0')
{
nRet += ((*string) - '0');
nRet *= 10;
string++;
}

if(bMinus)
nRet *= -1;

return (nRet/10);
}

"Marcelo Cortes" <ho...@sympatico.ca> wrote in message

news:AMoB7.11731$v21.2...@news20.bellglobal.com...

Pete Becker

unread,
Oct 24, 2001, 8:45:25 AM10/24/01
to
Marcelo Cortes wrote:
>
> I developed the bellow function for itoa. Could anyone see a way to make it
> faster? Does anyone see any mistake?

Try converting the value INT_MIN, base 10. Or the value 10, base 16.

gbayles

unread,
Oct 25, 2001, 12:51:25 PM10/25/01
to
Jack Klein <jack...@spamcop.net> wrote in message news:<mf47ttckldb9loq1v...@4ax.com>...
> On Sun, 21 Oct 2001 22:21:39 -0500, "Marcelo Cortes"
> <ho...@sympatico.ca> wrote in comp.lang.c++:
>
> > I have seen many interview question where they ask for a implementation of
> > itoa and atoi using C/C++. Could anyone send it to me?
>
> Given that itoa() is not a standard library function in either C or
> C++, and atoi() is a buggy, dangerous hack by definition, I would be
> leery of anyone who used such a question in a job interview situation.

But Jack, the factors you mention make it an excellent question for
a job interview IMHO. The response would reveal a great deal about
the candidate's approach to problems, working style, and language
knowledge. First of all, there are design issues. Which of the
multiple feasible prototypes for each function? Leading spaces? String
doesn't represent an integer? Other numeric bases? Numeric overflow?
Thread safety? Priorities - rapid implementation, performance,
safety?

There are many real world applications that require custom
conversions of object values to and from strings. Integer to/from
string is just the simplest example of this problem.

[snip]


> If you can't actually write and debug an implementation of either

> function inside of 10 minutes on a bad hair day, ...

I'd expect a good candidate to spend more time than that clarifying
the design issues.

gbayles

unread,
Oct 25, 2001, 1:27:59 PM10/25/01
to
"Marcelo Cortes" <ho...@sympatico.ca> wrote in message news:<W9pB7.11751$v21.2...@news20.bellglobal.com>...

> int MYatoi( const char *string )
> {
> bool bMinus = false;
> int nRet = 0;
>
> if( string[0] == '-')
What if string is null?

> {
> bMinus = true;
> string++;
> }
>
> while(*string != '\0')
> {
> nRet += ((*string) - '0');

What if the character isn't a valid digit?
What if the character is a leading space?

> nRet *= 10;
What if the string of digits is very long?
What if we want to handle other bases?

BTW, reverse the two previous steps and you won't have to
divide in the return statement. As is, this will cause
numeric overflow with some valid input strings.

Stuart Golodetz

unread,
Oct 26, 2001, 7:46:32 PM10/26/01
to
"Karl Heinz Buchegger" <kbuc...@gascad.at> wrote in message
news:3BD54953...@gascad.at...

Do you mean sscanf(str, "%d", &res); ?

HTH,

Stuart.

Marcelo Cortes

unread,
Oct 27, 2001, 12:18:14 PM10/27/01
to
"Stuart Golodetz" <sgol...@dial.pipex.com> wrote in message
news:3bd9f72c$0$8513$cc9e...@news.dial.pipex.com...

I am sorry, I forgot to mention that usually we can't use #include
<string.h>.

So made like this:

int MYatoi( const char *string )
{
bool bMinus = false;
int nRet = 0;

if( string[0] == '-')

{
bMinus = true;
string++;
}

while(*string != '\0')
{
if(*string < '0' || *string > '9')
return 0;

nRet += ((*string) - '0');

nRet *= 10;
string++;
}

if(bMinus)
nRet *= -1;

return (nRet/10);
}


>

0 new messages