?
Solved

Itoa algorithm?

Posted on 1997-10-15
2
Medium Priority
?
1,087 Views
Last Modified: 2008-03-03
I was wondering how you could (in c/c++ or any language) take an integer like 12345 and convert it into the string "12345" like the C function "Itoa" does.  It would be great if someone could give me step by step instructions or point me to somewhere on the web.  I couldn't find the algorithm myself.
0
Comment
Question by:tdiamond
[X]
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
2 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 400 total points
ID: 1171550
The basic algorithm is to take the number and divide it by 10. That gives you two components: quotient and remainder. The remainder is the number between 0 and 9 that you can convert a character by adding it to '0'. Repeat until the quotient is 0.

The only tricky part is that as you keep doing this, the remainder is given in backward order.

For example, let's use 12345.

  12345 / 10 = 1234
  12345 % 10 = 5       (% is modulus operator, gives remainder)

  1234 / 10 = 123
  1234 % 10 = 4

  123 / 10 = 12
  123 % 10 = 3

  12 / 10 = 1
  12 % 10 = 2

  1 / 10 = 0
  1 % 10 = 1


0
 

Author Comment

by:tdiamond
ID: 1171551
That is exactly what I needed!  Thank you VERY much.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 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