Advertisement
levartolona

C_PRG_LANG_EX_4.12

Feb 1st, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define ABS(n) n >= 0 ? n : -n
  5.  
  6. void my_itoa(int numb, char *s);
  7. void my_itoa_rec(int numb, char **s);
  8.  
  9. void my_itoa_rec(int numb, char **s)
  10. {
  11.     if (numb)
  12.         my_itoa_rec(numb / 10, s);
  13.     else
  14.     {
  15.         return;
  16.     }
  17.  
  18.     int digit = numb % 10;
  19.     digit = ABS(digit);
  20.     **s = digit + '0';
  21.     (*s)++;
  22.     **s = '\0';
  23.  
  24.     return;
  25. }
  26.  
  27. void my_itoa(int numb, char *s)
  28. {
  29.     if (!numb)
  30.     {
  31.         s[0] = '0';
  32.         s[1] = '\0';
  33.         return;
  34.     }
  35.     if (numb < 0)
  36.         *s++ = '-';
  37.  
  38.     my_itoa_rec(numb, &s);
  39. }
  40.  
  41. int main (void)
  42. {
  43.     int numb = INT_MAX;
  44.     char s[256];
  45.  
  46.     my_itoa(numb, s);
  47.     printf("%s\n", s);
  48.  
  49.     numb = INT_MIN;
  50.     my_itoa(numb, s);
  51.     printf("%s\n", s);
  52.  
  53.     numb = 0;
  54.     my_itoa(numb, s);
  55.     printf("%s\n", s);
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement