As i have written Atoi(); Now i have written Itoa.
Here I have used a method of getting the last digit stroring it in an array and then reversing the array to get the required answer.

I look forward for Improvements in this code.

``````/****************************************************************************
Function Name:: itoa(int ,char [] )
Written By:: 	Susheel Kumar
Date::		Saturday 02 May 2009
----------------------------------------------------
Objective:: Convert an Integer to a cstring.
----------------------------------------------------

========================
Input Values::
Arg1 :: int (The Integer Needed To Be Converted)
Arg2 :: char [] (A space for holding the cstring)
--------------
Returning Value::
Pointer to cstring;
--------------
=========================
Known Flaws::
Unknown Response to shortage in cstring space ;

I.e.:
char b[2];
itoa(54321,b);//This size difference isnt defined.

****************************************************************************/

#include <iostream>
#include <cstring>

char* itoa(int a, char b[]);	//Target Function;

//Functions Declarations
char get_char(int digit);	//Converter from digit ie 0123456789 to char '0'......'9'
void rev(char *);		//reverse function ;

//Functions
int main()	//Example Program
{
char b[10];
char* string=itoa(543215,b);
std::cout<<"string:: "<<string<<" B== "<<b<<"\n";

}

char* itoa(int number, char strrep[])
{
int count=0;
while(number!=0)
{
int dig=number%10;
number-=dig;
number/=10;
strrep[count]=get_char(dig);
count++;
}
strrep[count]=0;
rev(strrep);
return strrep;
}

char get_char(int digit)
{

char charstr[]="0123456789";
return charstr[digit];

}

void rev(char *p)
{
char *q=&p[strlen(p)-1];
char *r=p;
while(q>r)
{
char s=*q;
*q=*r;
*r=s;
q--;
r++;
}
}``````

If you are asking about ways you can improve it, if you look at itoa, it needs to be able to handle negative numbers and it needs to be able to handle different bases:

``char *  itoa ( int …``

## All 4 Replies

You could change your get_char function to: `char get_char(int n) {return n-'0';}` (edit:: unless your goal is to also provide conversions from other bases)

In your rev-function (see line 72) a for-statement would be better to use I think, it will also compact your code a bit :)
(You can use a for like this: `for(; q>r; q--, r++)` )

If you are asking about ways you can improve it, if you look at itoa, it needs to be able to handle negative numbers and it needs to be able to handle different bases:

``char *  itoa ( int value, char * str, int base );``
commented: You can *always* find an error :P +3

If you are asking about ways you can improve it, if you look at itoa, it needs to be able to handle negative numbers and it needs to be able to handle different bases:

``char *  itoa ( int value, char * str, int base );``

Agree with negative, disagree with base. Since it is a compiler-specific function, there is no 'official' definition. Therefore, adding base is simply a possible feature/addition. :icon_wink:

So This handles the Negitive Part As Well. I didnt write the function to handle bases because, I am answering questions in my Programming Book.

``````/****************************************************************************
Function Name:: itoa(int ,char [] )
Written By:: 	Susheel Kumar
Date::		Saturday 02 May 2009
----------------------------------------------------
Objective:: Convert an Integer to a cstring.
----------------------------------------------------

========================
Input Values::
Arg1 :: int (The Integer Needed To Be Converted)
Arg2 :: char [] (A space for holding the cstring)
--------------
Returning Value::
Pointer to cstring;
--------------
=========================
Known Flaws::
Unknown Response to shortage in cstring space ;

I.e.:
char b[2];
itoa(54321,b);//This size difference isnt defined.

****************************************************************************/

#include <iostream>
#include <cstring>

char* itoa(int a, char b[]);	//Target Function;

//Functions Declarations
char get_char(int digit);	//Converter from digit ie 0123456789 to char '0'......'9'
void rev(char *);		//reverse function ;

//Functions
int main()	//Example Program
{
char b[10];
char* string=itoa(-215,b);
std::cout<<"\nstring:: "<<string<<" B== "<<b<<"\n";

}

char* itoa(int number, char strrep[])
{
int count=0;
bool flag=true;
if(number<0)
{
flag=false;
}
while(number!=0)
{
int dig=number%10;

number-=dig;
number/=10;
if (flag==true)
{
strrep[count]=get_char(dig);
}
else
{
strrep[count]=get_char(-dig);
}
count++;
}
if(flag==false)
{
strrep[count]='-';
count++;
}
strrep[count]=0;
rev(strrep);
return strrep;
}

char get_char(int digit)
{
char charstr[]="0123456789";
return charstr[digit];

}

void rev(char *p)
{
char *q=&p[strlen(p)-1];
char *r=p;
for(;q>r;q--,r++)
{
char s=*q;
*q=*r;
*r=s;
}
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.