A function to return a string representation of an integer with a given width : Int Convert « Data Type « C / ANSI-C

C / ANSI-C
1. assert.h
2. Console
3. ctype.h
4. Data Structure Algorithm
5. Data Type
6. Development
7. File
8. Function
9. Language Basics
10. Macro Preprocessor
11. Math
12. math.h
13. Memory
14. Pointer
15. setjmp.h
16. signal.h
17. Small Application
18. stdio.h
19. stdlib.h
20. String
21. string.h
22. Structure
23. time.h
24. wctype.h
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C / ANSI-C » Data Type » Int ConvertScreenshots 
A function to return a string representation of an integer with a given width
A function to return a string representation of an integer with a given width

/*
Beginning C, Third Edition
 By Ivor Horton
 ISBN: 1-59059-253-0
 Published: Apr 2004
 Publisher: apress

*/

#include <stdio.h>

/* Convert an integer to a string with a fixed width.      */
/* if the widh is too small, the minimum width is assumed. */
char* itoa(int n, char str[]int width)
{
  int i = 0;               /* Loop counter              */
  int j = 0;               /* Loop counter              */
  int negative = 0;        /* Indicate negative integer */
  int length = 0;          /* Length of string          */
  int temp = 0;            /* Temporary storage         */
 
  if(negative = (n<0))     /* Is it negative?  */
    n = -n;                /* make it positive */

  /* Generate digit characters in reverse order */
  do
  {
    str[i++'0'+n%10;    /* Create a rightmost digit        */
    n /= 10;                /* Remove the digit                */              
  }while(n>0);              /* Go again if there's more digits */

  if(negative)              /* If it was negative */
    str[i++'-';         /* Append minus       */
  str[i'\0';            /* Append terminator  */
  length = i;               /* Save the length    */

  /* Now reverse the string in place */
  /* by switching first and last,    */
  /* second and last but one, etc    */
  for(i = ; i<length/;i++)
  {
    temp = str[i];
    str[i= str[length-i-1];
    str[length-i-1= temp;
  }

  /* Shift the string to the right and insert blanks */
  if(width>length)
  {
    for(i=length, j = width ; i>= ; i--, j--)
      str[j= str[i];
    for(i = ; i<width-length ; i++)
      str[i' ';
  }
  return str;                /* Return the string */
}


void main()
{
   char str[15];              /* Stores string representation of integer */
   long testdata[] 30L, -98L0L, -1L999L, -12345L};  
   int i = 0;                /* Loop control variable                    */

   for (i = ; i< sizeof testdata/sizeof(long; i++)
     printf("Integer value is %10d, string is %s\n", testdata[i], itoa(testdata[i],str, 14));
}


 

           
       
Related examples in the same category
1. A function to return a string representation of an integerA function to return a string representation of an integer
2. Convert an integer to words
3. Convert integer to string: how to use itoa
w__w___w_._j___a__va___2_s_._com_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.