home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c023 / 1.img / PROGRAMS / ITOA.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-11-05  |  1.0 KB  |  43 lines

  1. /*---------------------------------------------------------------------------- 
  2.  * itoa() : Integer to ascii conversion program.
  3.  * (This program is from p. 60 of the Kernighan and Ritchie text) 
  4.  *---------------------------------------------------------------------------*/
  5.  
  6. #include "cbtree.h"
  7.  
  8. void bt_itoa(n, s)  /* CBTREE version so it doesn't collide with Turbo Lib */
  9. int n;
  10. char s[];
  11. {
  12.     extern void reverse();
  13.     int i, sign;
  14.  
  15.     if ((sign = n) < 0)    /* record sign            */
  16.         n = -n;            /*  make n positive            */
  17.     i = 0;
  18.     do {        /* generate digits in reverse order */
  19.         s[i++] = n % 10 + '0';        /* get next digit */
  20.     } while ((n /= 10) >  0);        /* delete it        */
  21.  
  22.     if  (sign < 0)
  23.         s[i++] = '-';
  24.     s[i] = '\0';
  25.  
  26.     reverse(s);
  27. }
  28.  
  29.     /* (This program is from p. 59 of the Kernighan and Ritchie text)
  30.     */
  31. void reverse(s)        /* reverse string s in place    */
  32. char s[];
  33. {
  34.     extern int strlen();
  35.     int c, i, j;
  36.  
  37.     for (i = 0, j = strlen(s)-1; i <j; i++, j--) {
  38.         c = s[i];
  39.         s[i] = s[j];
  40.         s[j] = c;
  41.     }
  42. }
  43.