This site features a collection of common technical interview questions gathered by a group of programmers who have been through, and given, lots of technical interviews. There is an emphasis on C++ and game programming technical interviews, but most of the questions are relevant to any technical interview.
Implement itoa

Implement the standard I/O function itoa(). The itoa() function constructs a string representation of an integer. The parameters are:

value: the integer to be converted to string representation.
string: points to the buffer that is to hold resulting string.
radix: is the base of the number; must be in the range 2 – 36.

char *itoa(int value, char *string, int radix)

5 Comments so far
Leave a comment

i think it’s something like this…

int stridx = 0;
for (;;) {
int q, pow = 0;
do {
// since q is int it will only contain
// whole part of the quotient
q = value / pow(radix, pow);
} while (q >= radix);

if (q < 10)
string[stridx++] = q + ‘0’; // use digits
string[stridx++] = q + ‘a’; // use letters

value -= (q * pow(radix, pow));
if (value == 0) {
string[stridx] = ”; // null plug

This is using C++ syntax

char *itoa(int value, char *string, int radix)
// Unfortunately , there is no way to guarantee that string
// is long enough to hold the converted value. The caller
// of this function must guarantee this for us

// Check for negative values.
if (value < 0)
*string++ = ‘-‘;

// Compute number of digits needed
int numDigits = static_cast(log(value)/log(radix));
// Null terminate now so that we don’t forget
string[numDigits] = ”;

// Fill in the string
while (numDigits > 0)
// Get the value of the current digit.
int remainder = value % radix;

// Update value for next iteration
value /= radix;

// Convert value into ascii.
char asciiDigit;
if (remainder < 10)
asciiDigit = remainder + ‘0’;
asciiDigit = remainder + ‘a’;

// Fill in the current digit
*string[numDigits-1] = asciiDigit;

// Update iterator

Oops, in my post above, I forgot to get the absolute “value”:

// Check for negative values.
if (value < 0)
*string++ = ‘-’;
value *= -1;

System::Convert::ToString(val); /snicker

My “boolean” values often comes from grilas form postings (checkboxes) and the values can be null. “True” values (checked) come back as “on” which, is a bit odd. So my boolean conversion isdef obtainBoolean(value) { if (value == null) return false final String b = “$value”.trim().toLowerCase() return b == “t” || b == “true” || b == “y” || b == “yes” || b == “on” || b == “1”}

Leave a comment
If you are including code in your comment, place it within a <div class='code'></div> tag for better formatting.

Do you have a technical interview question you would like to submit? Some tips you would like to pass on? Just want to say hi? Feel free to contact us