Idea: To first determine the length of an integer, there are two benefits to doing so.
- One is used to save memory because the string address space should be applied in advance;
- One is the length, you can directly take the high number of integers, do not need to be taken from the single-digit, from the character of the string composed of a reverse order
The specific code is as follows:
/** integers converted into strings*/#include<stdio.h>#include<stdlib.h>#include<math.h>Char*inttostring (inta) { intTemp=a;//Save the value of a intLen//Cache Land intb=a%Ten; intCount=0;//calculate the length of an integer Char*string; while(b!=0) {Count++; A=a/Ten; b=a%Ten; if(b==a) Break; } Count++; string=(Char*)malloc(count+1); b=temp%Ten; Len=count; printf ("count=%d\n", Count); while(temp) {a=TEMP/MYPOW10 (--count); (*string+ +) =a+'0'; Temp=temp-a*MYPOW10 (count); } *string=0; string=string-Len; return string;}intMYPOW10 (inta) { if(a<0) return 0; intI=1; intsum=1; for(i=1; i<=a;i++) {sum=sum*Ten; } returnsum;}intMainvoid){ intA=123; printf ("Please enter an integer: \ n"); scanf ("%d",&a); Char*b=inttostring (a); printf ("%s\n", B); return 0;}
Integer conversion string