Integer conversion string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.