Posted By

priyanhere on 11/15/06


Tagged

String c integer itoa freebookzone com


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

copyleft
khouser


(Integer to String ) itoa


 / Published in: C++
 

URL: http://www.freebookzone.com/others/itoa.h

  1. /* ========================================================================== */
  2. /* */
  3. /* itoa.h */
  4. /* (c) 2006 Priyadarsan */
  5. /* */
  6. /* a program to convert integer to string */
  7. /* (Original Code: http://www.freebookzone.com/others/itoa.h) */
  8. /* Ex. usage file available at http://www.freebookzone.com/others/itoa.c */
  9. /* */
  10. /* ========================================================================== */
  11.  
  12. char* itoa (int n){
  13. int i=0,j;
  14. char* s;
  15. char* u;
  16.  
  17. s= (char*) malloc(17);
  18. u= (char*) malloc(17);
  19.  
  20. do{
  21. s[i++]=(char)( n%10+48 );
  22. n-=n%10;
  23. }
  24. while((n/=10)>0);
  25. for (j=0;j<i;j++)
  26. u[i-1-j]=s[j];
  27.  
  28. u[j]='\0';
  29. return u;
  30. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: the_coder on June 4, 2008

how about just

char* itoa (int n) {

  char* result = malloc(17);

  sprintf(result, "%d", n);

  return result;

}

You need to login to post a comment.