Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C++ int to char[], there is something wrong with my own function, please ask!
C++ int to char[], there is something wrong with my own function, please ask!
I changed it for you. I'm not afraid of how many zeros there are. But I have a simpler integer-to-string view of the last itostr(int a, s[]);

# include & ltcstdio & gt

# include & ltcstdlib & gt

# include & ltiostream & gt

# include & lt string & gt

Use namespace std

//Get integer digits

Integer number (integer x)

{

int sum = 1;

for(int I = x; (i/= 10)>0; sum++);

Returns the sum;

}

void Int2Str(int a,char sz[])

{

//Required number

int num = a;

//cycle length

int length = digit(a);

//digits

Int num_digit = length;

Int prefix;

for(int I = 0; I < length; i++)

{

Num_digit = digit (number);

Pre_num_digit = number;

int m = 1;

int n = 1;

int count = 0;

for(; num _ digit & gt 1; num_digit -)

{

m * = 10;

n * = 10;

}

m = num/m;

num-= n * m;

SZ[I]= m+48;

count = pre _ num _ digit-digit(num)- 1;

while(count & gt; 0)

{

SZ[++ I]= 48;

count-;

}

}

SZ[I]= ' \ 0 ';

}

void main()

{

char SZ[ 100];

Int2Str(7000456,SZ);

cout & lt& ltsz & lt& ltendl

System ("suspended");

}

//I wrote an integer to string, which can be converted into a signed integer.

void itostr(int n,char s[])

{

int i,sign

if((sign = n)& lt; 0)n =-n;

I = 0;

do

{

s[i++]= n % 10+' 0 ';

}while ( (n/= 10)>0);

if(sign & lt; 0)s[i++]= '-';

s[I]= ' \ 0 ';

//Here is the string inversion.

char * p = s;

char * q = s+strlen(s)- 1;

char t;

while(p & lt; Q)

{

t = * p;

* p = * q;

* q = t;

p++;

q-;

}