Thursday, November 29, 2007

itoa

Sorry to anyone who doesn't like the programming puzzles...

Write an itoa function in C w/o using sprintf or the (non)standard itoa.

itoa takes in an integer and returns a character array representation of that integer, ex: 1234 -> ['1', '2', '3', '4']

If you're not a programmer you can spell out your solution in words, buts its way easier that way...

I'll share my solution in the comments later if you want it

3 comments:

Benjamin P Lee said...

My first stab at a solution can be seen here.

Instead of using the log10 you could iteratively find the number of digits, or use a dynamic structure to find all of your char's and then put them into an array.

Please take it with a grain of salt ... its been a long time since I have done any C work.

Also, I have to thank this website for use of its online C compiler and this site for the use of there online code-to-html converter. Very nice tools.

Kevin Berridge said...

Yep, same here, only I didn't know the log10 trick, so I just /= 10 until the number goes to 0. And I make my initial char array big enough to support any size integer. That also means that I have to reverse the string before I return it. That being said, I like the log10 trick.

Josh Schramm said...

Boo for googlepages being blocked. Stupid blocking stuff.