[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Itoa algorithm?

Posted on 1997-10-15
Medium Priority
1,089 Views
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
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

LVL 15

Accepted Solution

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

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

## Featured Post

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month13 days, 2 hours left to enroll