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.
> 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
"Jack Klein" <jack...@spamcop.net> wrote in message
news:mf47ttckldb9loq1v...@4ax.com...
Don't top post, it is against Internet guidelines and considered rude
here.
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>...
///
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.
>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"
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
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)
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;
}
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...
Try converting the value INT_MIN, base 10. Or the value 10, base 16.
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.
> {
> 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.
Do you mean sscanf(str, "%d", &res); ?
HTH,
Stuart.
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);
}
>